package main import ( "log" "github.com/valyala/fasthttp" "git.loafle.net/overflow/overflow_gateway_web/handler/file" "git.loafle.net/overflow/overflow_gateway_web/handler/web" gws "git.loafle.net/overflow/overflow_gateway_websocket" ) func main() { o := &gws.ServerOptions{ OnConnection: onConnection, OnDisconnected: onDisconnected, OnCheckOrigin: onCheckOrigin, } s := gws.NewServer(o) wh := web.New() fh := file.New() s.HandleSocket("/web", wh.GetSocketOption()) s.HandleSocket("/file", fh.GetSocketOption()) s.ListenAndServe(":19090") } func onCheckOrigin(ctx *fasthttp.RequestCtx) bool { if origin := string(ctx.Request.Header.Peek("Origin")); origin != "" { ctx.Response.Header.Set("Access-Control-Allow-Origin", origin) if string(ctx.Method()) == "OPTIONS" && string(ctx.Request.Header.Peek("Access-Control-Request-Method")) != "" { ctx.Response.Header.Set("Access-Control-Allow-Headers", "Content-Type, Accept") ctx.Response.Header.Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE") } } return true } func onConnection(soc gws.Socket) { log.Printf("connect: path: %s, id:%s\n", soc.Path(), soc.ID()) } func onDisconnected(soc gws.Socket) { log.Printf("disconnect: path: %s, id:%s\n", soc.Path(), soc.ID()) }