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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"golang.org/x/net/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Actions struct {
|
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
|
actions.Functions[name] = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,6 @@ func (actions Actions) has(name string) bool {
|
||||||
return exists
|
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)
|
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/daaku/go.zipexe v1.0.2 // indirect
|
||||||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 // indirect
|
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 // indirect
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible // 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/jezek/xgb v1.1.0 // indirect
|
||||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 // indirect
|
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 // indirect
|
||||||
github.com/labstack/echo/v4 v4.11.1 // 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/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 h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
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/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 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||||
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
"github.com/kbinani/screenshot"
|
"github.com/kbinani/screenshot"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"golang.org/x/net/websocket"
|
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -44,7 +44,7 @@ type MessageResponse struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSimpleMessageValue(msg string) string {
|
func getSimpleMessageValue(msg []byte) string {
|
||||||
data := SimpleMessageData{}
|
data := SimpleMessageData{}
|
||||||
json.Unmarshal([]byte(msg), &data)
|
json.Unmarshal([]byte(msg), &data)
|
||||||
|
|
||||||
|
@ -53,15 +53,15 @@ func getSimpleMessageValue(msg string) string {
|
||||||
|
|
||||||
func sendMessageResponse(ws *websocket.Conn, r MessageResponse) {
|
func sendMessageResponse(ws *websocket.Conn, r MessageResponse) {
|
||||||
value, _ := json.Marshal(r)
|
value, _ := json.Marshal(r)
|
||||||
websocket.Message.Send(ws, string(value))
|
ws.WriteMessage(websocket.TextMessage, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createActions() Actions {
|
func createActions() Actions {
|
||||||
actions := 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{}
|
data := PointerMessageData{}
|
||||||
json.Unmarshal([]byte(msg), &data)
|
json.Unmarshal([]byte(msg), &data)
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ func createActions() Actions {
|
||||||
return cmd.Run()
|
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)
|
value := getSimpleMessageValue(msg)
|
||||||
key := ""
|
key := ""
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ func createActions() Actions {
|
||||||
return nil
|
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)
|
value := getSimpleMessageValue(msg)
|
||||||
|
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
@ -140,7 +140,7 @@ func createActions() Actions {
|
||||||
return cmd.Run()
|
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)
|
value := getSimpleMessageValue(msg)
|
||||||
|
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
@ -177,7 +177,7 @@ func createActions() Actions {
|
||||||
return cmd.Run()
|
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)
|
value := getSimpleMessageValue(msg)
|
||||||
|
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
@ -237,7 +237,7 @@ func createActions() Actions {
|
||||||
return nil
|
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))
|
value := strings.TrimSpace(getSimpleMessageValue(msg))
|
||||||
|
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
@ -269,7 +269,7 @@ func createActions() Actions {
|
||||||
return cmd.Run()
|
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))
|
value := strings.TrimSpace(getSimpleMessageValue(msg))
|
||||||
keys := make(map[string]string)
|
keys := make(map[string]string)
|
||||||
|
|
||||||
|
@ -294,7 +294,7 @@ func createActions() Actions {
|
||||||
return cmd.Run()
|
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))
|
value := strings.TrimSpace(getSimpleMessageValue(msg))
|
||||||
|
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
@ -306,7 +306,7 @@ func createActions() Actions {
|
||||||
return cmd.Run()
|
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{}
|
data := ScreenshotMessageData{}
|
||||||
json.Unmarshal([]byte(msg), &data)
|
json.Unmarshal([]byte(msg), &data)
|
||||||
|
|
||||||
|
@ -336,7 +336,7 @@ func createActions() Actions {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
actions.add("messages", func(ws *websocket.Conn, msg string) error {
|
actions.add("messages", func(ws *websocket.Conn, msg []byte) error {
|
||||||
data := MessagesData{}
|
data := MessagesData{}
|
||||||
json.Unmarshal([]byte(msg), &data)
|
json.Unmarshal([]byte(msg), &data)
|
||||||
|
|
||||||
|
@ -344,7 +344,7 @@ func createActions() Actions {
|
||||||
msg, _ := json.Marshal(value)
|
msg, _ := json.Marshal(value)
|
||||||
|
|
||||||
if actions.has(value.Type) {
|
if actions.has(value.Type) {
|
||||||
actions.exec(value.Type, ws, string(msg))
|
actions.exec(value.Type, ws, msg)
|
||||||
time.Sleep(400 * time.Millisecond)
|
time.Sleep(400 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -355,21 +355,32 @@ func createActions() Actions {
|
||||||
return actions
|
return actions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
upgrader = websocket.Upgrader{}
|
||||||
|
)
|
||||||
|
|
||||||
func wsController(c echo.Context) error {
|
func wsController(c echo.Context) error {
|
||||||
websocket.Handler(func(ws *websocket.Conn) {
|
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
|
||||||
defer ws.Close()
|
|
||||||
for {
|
|
||||||
msg := ""
|
|
||||||
websocket.Message.Receive(ws, &msg)
|
|
||||||
|
|
||||||
message := Message{}
|
if err != nil {
|
||||||
json.Unmarshal([]byte(msg), &message)
|
return err
|
||||||
|
}
|
||||||
|
defer ws.Close()
|
||||||
|
|
||||||
if message.Type != "" && actions.has(message.Type) {
|
for {
|
||||||
actions.exec(message.Type, ws, msg)
|
_, 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue