mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
Add socket.io example
Former-commit-id: 895669c537db46f1694e8006418ea0509c60cb04
This commit is contained in:
54
_examples/websocket/socketio/main.go
Normal file
54
_examples/websocket/socketio/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// 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),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user