mirror of
https://github.com/kataras/iris.git
synced 2025-01-25 03:31:04 +01:00
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
|
// Package main runs a go-socket.io based websocket server.
|
||
|
// An Iris compatible clone of: https://github.com/googollee/go-socket.io#example,
|
||
|
// use of `iris.FromStd` to convert its handler.
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
socketio "github.com/googollee/go-socket.io"
|
||
|
"github.com/kataras/iris/v12"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
app := iris.New()
|
||
|
server, err := socketio.NewServer(nil)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
server.OnConnect("/", func(s socketio.Conn) error {
|
||
|
s.SetContext("")
|
||
|
fmt.Println("connected:", s.ID())
|
||
|
return nil
|
||
|
})
|
||
|
server.OnEvent("/", "notice", func(s socketio.Conn, msg string) {
|
||
|
fmt.Println("notice:", msg)
|
||
|
s.Emit("reply", "have "+msg)
|
||
|
})
|
||
|
server.OnEvent("/chat", "msg", func(s socketio.Conn, msg string) string {
|
||
|
s.SetContext(msg)
|
||
|
return "recv " + msg
|
||
|
})
|
||
|
server.OnEvent("/", "bye", func(s socketio.Conn) string {
|
||
|
last := s.Context().(string)
|
||
|
s.Emit("bye", last)
|
||
|
s.Close()
|
||
|
return last
|
||
|
})
|
||
|
server.OnError("/", func(s socketio.Conn, e error) {
|
||
|
fmt.Println("meet error:", e)
|
||
|
})
|
||
|
server.OnDisconnect("/", func(s socketio.Conn, reason string) {
|
||
|
fmt.Println("closed", reason)
|
||
|
})
|
||
|
go server.Serve()
|
||
|
defer server.Close()
|
||
|
|
||
|
app.HandleMany("GET POST", "/socket.io/{any:path}", iris.FromStd(server))
|
||
|
app.HandleDir("/", "./asset")
|
||
|
app.Run(iris.Addr(":8000"),
|
||
|
iris.WithoutPathCorrection,
|
||
|
iris.WithoutServerError(iris.ErrServerClosed),
|
||
|
)
|
||
|
}
|