2019-02-09 03:28:00 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kataras/iris"
|
|
|
|
"github.com/kataras/iris/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
ws := websocket.New(websocket.Config{})
|
|
|
|
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())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-02-10 16:16:43 +01:00
|
|
|
app.Get("/socket", ws.Handler())
|
|
|
|
|
2019-02-09 03:28:00 +01:00
|
|
|
app.Run(iris.Addr(":8080"))
|
|
|
|
}
|