ws works
This commit is contained in:
Generated
+10
@@ -0,0 +1,10 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Ignored default folder with query files
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
Generated
+9
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Generated
+11
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GoImports">
|
||||
<option name="excludedPackages">
|
||||
<array>
|
||||
<option value="github.com/pkg/errors" />
|
||||
<option value="golang.org/x/net/context" />
|
||||
</array>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/go-socket.iml" filepath="$PROJECT_DIR$/.idea/go-socket.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,2 @@
|
||||
nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y=
|
||||
nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
|
||||
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
"nhooyr.io/websocket/wsjson"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
OnOpen func(conn *websocket.Conn)
|
||||
OnClose func(conn *websocket.Conn, err error)
|
||||
OnMessage func(conn *websocket.Conn, msg map[string]any)
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
|
||||
InsecureSkipVerify: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("accept error:", err)
|
||||
return
|
||||
}
|
||||
defer conn.CloseNow()
|
||||
|
||||
if s.OnOpen != nil {
|
||||
s.OnOpen(conn)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var readErr error
|
||||
for {
|
||||
var msg map[string]any
|
||||
if readErr = wsjson.Read(ctx, conn, &msg); readErr != nil {
|
||||
break
|
||||
}
|
||||
if s.OnMessage != nil {
|
||||
s.OnMessage(conn, msg)
|
||||
}
|
||||
}
|
||||
|
||||
if s.OnClose != nil {
|
||||
s.OnClose(conn, readErr)
|
||||
}
|
||||
|
||||
conn.Close(websocket.StatusNormalClosure, "done")
|
||||
}
|
||||
|
||||
func main() {
|
||||
srv := &Server{
|
||||
OnOpen: func(conn *websocket.Conn) {
|
||||
log.Println("client connected")
|
||||
},
|
||||
OnClose: func(conn *websocket.Conn, err error) {
|
||||
log.Println("client disconnected:", err)
|
||||
},
|
||||
OnMessage: func(conn *websocket.Conn, msg map[string]any) {
|
||||
log.Printf("received: %v\n", msg)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := wsjson.Write(ctx, conn, map[string]any{"echo": msg}); err != nil {
|
||||
log.Println("write error:", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
http.Handle("/ws", srv)
|
||||
log.Println("server listening on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
Reference in New Issue
Block a user