replace golang.org/x/net/websocket with github.com/gorilla/websocket
This commit is contained in:
parent
6d6bfee742
commit
676e11db1c
|
@ -1,14 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/net/websocket"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type Actions struct {
|
||||
Functions map[string]func(ws *websocket.Conn, msg string) error
|
||||
Functions map[string]func(ws *websocket.Conn, msg []byte) error
|
||||
}
|
||||
|
||||
func (actions Actions) add(name string, callback func(ws *websocket.Conn, msg string) error) {
|
||||
func (actions Actions) add(name string, callback func(ws *websocket.Conn, msg []byte) error) {
|
||||
actions.Functions[name] = callback
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,6 @@ func (actions Actions) has(name string) bool {
|
|||
return exists
|
||||
}
|
||||
|
||||
func (actions Actions) exec(name string, ws *websocket.Conn, msg string) error {
|
||||
func (actions Actions) exec(name string, ws *websocket.Conn, msg []byte) error {
|
||||
return actions.Functions[name](ws, msg)
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -7,6 +7,7 @@ require (
|
|||
github.com/daaku/go.zipexe v1.0.2 // indirect
|
||||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 // indirect
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/jezek/xgb v1.1.0 // indirect
|
||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 // indirect
|
||||
github.com/labstack/echo/v4 v4.11.1 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -10,6 +10,8 @@ github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 h1:VLEKvjGJYAMCXw0/3
|
|||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7/go.mod h1:uF6rMu/1nvu+5DpiRLwusA6xB8zlkNoGzKn8lmYONUo=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||
|
|
|
@ -5,9 +5,9 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/kbinani/screenshot"
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/net/websocket"
|
||||
"image/jpeg"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
|
@ -44,7 +44,7 @@ type MessageResponse struct {
|
|||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func getSimpleMessageValue(msg string) string {
|
||||
func getSimpleMessageValue(msg []byte) string {
|
||||
data := SimpleMessageData{}
|
||||
json.Unmarshal([]byte(msg), &data)
|
||||
|
||||
|
@ -53,15 +53,15 @@ func getSimpleMessageValue(msg string) string {
|
|||
|
||||
func sendMessageResponse(ws *websocket.Conn, r MessageResponse) {
|
||||
value, _ := json.Marshal(r)
|
||||
websocket.Message.Send(ws, string(value))
|
||||
ws.WriteMessage(websocket.TextMessage, value)
|
||||
}
|
||||
|
||||
func createActions() Actions {
|
||||
actions := Actions{
|
||||
Functions: make(map[string]func(ws *websocket.Conn, msg string) error),
|
||||
Functions: make(map[string]func(ws *websocket.Conn, msg []byte) error),
|
||||
}
|
||||
|
||||
actions.add("pointer", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("pointer", func(ws *websocket.Conn, msg []byte) error {
|
||||
data := PointerMessageData{}
|
||||
json.Unmarshal([]byte(msg), &data)
|
||||
|
||||
|
@ -108,7 +108,7 @@ func createActions() Actions {
|
|||
return cmd.Run()
|
||||
})
|
||||
|
||||
actions.add("scroll", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("scroll", func(ws *websocket.Conn, msg []byte) error {
|
||||
value := getSimpleMessageValue(msg)
|
||||
key := ""
|
||||
|
||||
|
@ -128,7 +128,7 @@ func createActions() Actions {
|
|||
return nil
|
||||
})
|
||||
|
||||
actions.add("workspace", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("workspace", func(ws *websocket.Conn, msg []byte) error {
|
||||
value := getSimpleMessageValue(msg)
|
||||
|
||||
if value == "" {
|
||||
|
@ -140,7 +140,7 @@ func createActions() Actions {
|
|||
return cmd.Run()
|
||||
})
|
||||
|
||||
actions.add("volume", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("volume", func(ws *websocket.Conn, msg []byte) error {
|
||||
value := getSimpleMessageValue(msg)
|
||||
|
||||
if value == "" {
|
||||
|
@ -177,7 +177,7 @@ func createActions() Actions {
|
|||
return cmd.Run()
|
||||
})
|
||||
|
||||
actions.add("media", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("media", func(ws *websocket.Conn, msg []byte) error {
|
||||
value := getSimpleMessageValue(msg)
|
||||
|
||||
if value == "" {
|
||||
|
@ -237,7 +237,7 @@ func createActions() Actions {
|
|||
return nil
|
||||
})
|
||||
|
||||
actions.add("keys", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("keys", func(ws *websocket.Conn, msg []byte) error {
|
||||
value := strings.TrimSpace(getSimpleMessageValue(msg))
|
||||
|
||||
if value == "" {
|
||||
|
@ -269,7 +269,7 @@ func createActions() Actions {
|
|||
return cmd.Run()
|
||||
})
|
||||
|
||||
actions.add("key", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("key", func(ws *websocket.Conn, msg []byte) error {
|
||||
value := strings.TrimSpace(getSimpleMessageValue(msg))
|
||||
keys := make(map[string]string)
|
||||
|
||||
|
@ -294,7 +294,7 @@ func createActions() Actions {
|
|||
return cmd.Run()
|
||||
})
|
||||
|
||||
actions.add("text", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("text", func(ws *websocket.Conn, msg []byte) error {
|
||||
value := strings.TrimSpace(getSimpleMessageValue(msg))
|
||||
|
||||
if value == "" {
|
||||
|
@ -306,7 +306,7 @@ func createActions() Actions {
|
|||
return cmd.Run()
|
||||
})
|
||||
|
||||
actions.add("screenshot", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("screenshot", func(ws *websocket.Conn, msg []byte) error {
|
||||
data := ScreenshotMessageData{}
|
||||
json.Unmarshal([]byte(msg), &data)
|
||||
|
||||
|
@ -336,7 +336,7 @@ func createActions() Actions {
|
|||
return nil
|
||||
})
|
||||
|
||||
actions.add("messages", func(ws *websocket.Conn, msg string) error {
|
||||
actions.add("messages", func(ws *websocket.Conn, msg []byte) error {
|
||||
data := MessagesData{}
|
||||
json.Unmarshal([]byte(msg), &data)
|
||||
|
||||
|
@ -344,7 +344,7 @@ func createActions() Actions {
|
|||
msg, _ := json.Marshal(value)
|
||||
|
||||
if actions.has(value.Type) {
|
||||
actions.exec(value.Type, ws, string(msg))
|
||||
actions.exec(value.Type, ws, msg)
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
@ -355,21 +355,32 @@ func createActions() Actions {
|
|||
return actions
|
||||
}
|
||||
|
||||
var (
|
||||
upgrader = websocket.Upgrader{}
|
||||
)
|
||||
|
||||
func wsController(c echo.Context) error {
|
||||
websocket.Handler(func(ws *websocket.Conn) {
|
||||
defer ws.Close()
|
||||
for {
|
||||
msg := ""
|
||||
websocket.Message.Receive(ws, &msg)
|
||||
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
|
||||
|
||||
message := Message{}
|
||||
json.Unmarshal([]byte(msg), &message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer ws.Close()
|
||||
|
||||
if message.Type != "" && actions.has(message.Type) {
|
||||
actions.exec(message.Type, ws, msg)
|
||||
}
|
||||
for {
|
||||
_, msg, err := ws.ReadMessage()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}).ServeHTTP(c.Response(), c.Request())
|
||||
|
||||
message := Message{}
|
||||
json.Unmarshal([]byte(msg), &message)
|
||||
|
||||
if message.Type != "" && actions.has(message.Type) {
|
||||
actions.exec(message.Type, ws, msg)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue