iris/_examples/websocket/go-client/server/main.go
Gerasimos (Makis) Maropoulos 280872fd59 add iris websocket client side for Go and a simple chat example
Former-commit-id: af1c555b6b092a3d0484fee2e200fd8767d7239e
2019-02-09 04:28:00 +02:00

33 lines
583 B
Go

package main
import (
"fmt"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/websocket"
)
func main() {
app := iris.New()
ws := websocket.New(websocket.Config{})
app.Get("/socket", ws.Handler())
ws.OnConnection(func(c websocket.Connection) {
go func() {
<-time.After(20 * time.Second)
c.Disconnect()
}()
c.On("chat", func(message string) {
c.To(websocket.Broadcast).Emit("chat", c.ID()+": "+message)
})
c.OnDisconnect(func() {
fmt.Printf("Connection with ID: %s has been disconnected!\n", c.ID())
})
})
app.Run(iris.Addr(":8080"))
}