Specify transports in v3? #112
-
|
@zishang520 just noticed you've started a nice consolidation of the code for v3. I'm trying to migrate my service to explore it and I don't see how to configure the WebTransport transport. Do you mind making a README on this part, assuming that is still supported with v3? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Compared to V2, V3 has optimized and fixed many issues, while the other functionalities remain the same as in V2. However, some work in V3 is still unfinished (such as example code and bug fixes), so it will take some time before the official release. package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/zishang520/socket.io/servers/engine/v3"
"github.com/zishang520/socket.io/servers/socket/v3"
"github.com/zishang520/socket.io/v3/pkg/log"
"github.com/zishang520/socket.io/v3/pkg/types"
"github.com/zishang520/socket.io/v3/pkg/webtransport"
)
func main() {
log.DEBUG = true
c := socket.DefaultServerOptions()
c.SetServeClient(true)
// c.SetConnectionStateRecovery(&socket.ConnectionStateRecovery{})
// c.SetAllowEIO3(true)
c.SetPingInterval(300 * time.Millisecond)
c.SetPingTimeout(200 * time.Millisecond)
c.SetMaxHttpBufferSize(1000000)
c.SetConnectTimeout(1000 * time.Millisecond)
c.SetTransports(types.NewSet(engine.Polling, engine.WebTransport))
c.SetCors(&types.Cors{
Origin: "*",
Credentials: true,
})
socketio := socket.NewServer(nil, nil)
socketio.On("connection", func(clients ...interface{}) {
client := clients[0].(*socket.Socket)
client.On("message", func(args ...interface{}) {
client.Emit("message-back", args...)
})
client.Emit("auth", client.Handshake().Auth)
client.On("message-with-ack", func(args ...interface{}) {
ack := args[len(args)-1].(socket.Ack)
ack(args[:len(args)-1], nil)
})
})
socketio.Of("/custom", nil).On("connection", func(clients ...interface{}) {
client := clients[0].(*socket.Socket)
client.Emit("auth", client.Handshake().Auth)
})
app := gin.Default()
// app.Put("/socket.io", adaptor.HTTPHandler(socketio.ServeHandler(c))) // test
app.POST("/socket.io/*f", gin.WrapH(socketio.ServeHandler(c)))
app.GET("/socket.io/*f", gin.WrapH(socketio.ServeHandler(c)))
go app.Run(":80")
// WebTransport start
// WebTransport uses udp, so you need to enable the new service.
customServer := types.NewWebServer(nil)
// A certificate is required and cannot be a self-signed certificate.
wts := customServer.ListenWebTransportTLS(":443", "domain.cer", "domain.key", nil, nil)
// Here is the core logic of the WebTransport handshake.
customServer.HandleFunc(socketio.Path()+"/", func(w http.ResponseWriter, r *http.Request) {
if webtransport.IsWebTransportUpgrade(r) {
// You need to call socketio.ServeHandler(nil) before this, otherwise you cannot get the Engine instance.
socketio.Engine().(engine.Server).OnWebTransportSession(types.NewHttpContext(w, r), wts)
} else {
customServer.DefaultHandler.ServeHTTP(w, r)
}
})
// WebTransport end
exit := make(chan struct{})
SignalC := make(chan os.Signal)
signal.Notify(SignalC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
for s := range SignalC {
switch s {
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
close(exit)
return
}
}
}()
<-exit
socketio.Close(nil)
os.Exit(0)
} |
Beta Was this translation helpful? Give feedback.
Compared to V2, V3 has optimized and fixed many issues, while the other functionalities remain the same as in V2. However, some work in V3 is still unfinished (such as example code and bug fixes), so it will take some time before the official release.
Below is the currently available sample code: