From 01d5578c7f8faa8e80b4dc7ac3687e33f09737e3 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 8 Aug 2018 14:45:39 +0300 Subject: [PATCH] add websocket example on README.md Former-commit-id: 18f44e0c682e189859f437d7c179bb8d3b4882da --- README.md | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3a2d3b95..00a6ca97 100644 --- a/README.md +++ b/README.md @@ -482,7 +482,7 @@ type User struct { Age uint8 `json:"age" validate:"gte=0,lte=130"` Email string `json:"email" validate:"required,email"` FavouriteColor string `json:"favColor" validate:"hexcolor|rgb|rgba"` - Addresses []*Address `json:"addresses" validate:"required,dive,required"` // a person can have a home and cottage... + Addresses []*Address `json:"addresses" validate:"required,dive,required"` } // Address houses a users address information. @@ -511,7 +511,8 @@ func main() { // Handle error. } - // Returns InvalidValidationError for bad validation input, nil or ValidationErrors ( []FieldError ) + // Returns InvalidValidationError for bad validation input, + // nil or ValidationErrors ( []FieldError ) err := validate.Struct(user) if err != nil { @@ -529,8 +530,8 @@ func main() { fmt.Println() fmt.Println(err.Namespace()) fmt.Println(err.Field()) - fmt.Println(err.StructNamespace()) // Can differ when a custom TagNameFunc is registered or. - fmt.Println(err.StructField()) // By passing alt name to ReportError like below. + fmt.Println(err.StructNamespace()) + fmt.Println(err.StructField()) fmt.Println(err.Tag()) fmt.Println(err.ActualTag()) fmt.Println(err.Kind()) @@ -575,6 +576,115 @@ func UserStructLevelValidation(sl validator.StructLevel) { } ``` +### Websockets + +```go +package main + +import ( + "fmt" + + "github.com/kataras/iris" + "github.com/kataras/iris/websocket" +) + +func main() { + app := iris.New() + + app.Get("/", func(ctx iris.Context) { + ctx.ServeFile("websockets.html", false) // second parameter: enable gzip? + }) + + setupWebsocket(app) + + // x2 + // http://localhost:8080 + // http://localhost:8080 + // write something, press submit, see the result. + app.Run(iris.Addr(":8080")) +} + +func setupWebsocket(app *iris.Application) { + // create our echo websocket server + ws := websocket.New(websocket.Config{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + }) + ws.OnConnection(handleConnection) + + // register the server on an endpoint. + // see the inline javascript code in the websockets.html, this endpoint is used to connect to the server. + app.Get("/echo", ws.Handler()) + // serve the javascript built'n client-side library, + // see websockets.html script tags, this path is used. + app.Any("/iris-ws.js",websocket.ClientHandler()) +} + +func handleConnection(c websocket.Connection) { + // Read events from browser + c.On("chat", func(msg string) { + // Print the message to the console, c.Context() is the iris's http context. + fmt.Printf("%s sent: %s\n", c.Context().RemoteAddr(), msg) + // Write message back to the client message owner with: + // c.Emit("chat", msg) + // Write message to all except this client with: + c.To(websocket.Broadcast).Emit("chat", msg) + }) +} +``` + +**websockets.html** + +```html + + + + + + + +

+
+
+
+
+```
+
+Navigate to the [_examples/websocket](_examples/websocket) folder for more.
+
 ### Cookies
 
 > Are you looking about [http sessions instead?](_examples/sessions)