Fix json errors

This commit is contained in:
qowevisa 2024-06-07 12:19:43 +03:00
parent 666bfbea4b
commit f25659a4a6

View File

@ -3,13 +3,14 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/binary"
"fmt" "fmt"
"log" "log"
"net" "net"
"net/url" "net/url"
"os" "os"
"git.qowevisa.me/Qowevisa/gotell/communication" com "git.qowevisa.me/Qowevisa/gotell/communication"
"git.qowevisa.me/Qowevisa/gotell/env" "git.qowevisa.me/Qowevisa/gotell/env"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
@ -69,7 +70,7 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
log.Printf("client: read: %s", err) log.Printf("client: read: %s", err)
return return
} }
msg, err := communication.Decode(buf[:n]) msg, err := com.Decode(buf[:n])
if err != nil { if err != nil {
log.Printf("ERROR: %#v\n", err) log.Printf("ERROR: %#v\n", err)
continue continue
@ -78,18 +79,26 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
continue continue
} }
log.Printf("client: received message from server: %v", *msg) log.Printf("client: received message from server: %v", *msg)
switch msg.ID {
case com.ID_SERVER_APPROVE_CLIENT_NICKNAME:
newID := binary.BigEndian.Uint16(msg.Data)
msg.FromID = newID
msg.Data = []byte{}
}
log.Printf("client: sending message to websocket: %v", *msg)
ws.WriteJSON(*msg) ws.WriteJSON(*msg)
} }
} }
func readFromWebSocket(conn net.Conn, ws *websocket.Conn) { func readFromWebSocket(conn net.Conn, ws *websocket.Conn) {
for { for {
var msg communication.Message var msg com.Message
err := ws.ReadJSON(&msg) err := ws.ReadJSON(&msg)
if err != nil { if err != nil {
log.Printf("WebSocket read error: %s", err) log.Printf("WebSocket read error: %s", err)
return return
} }
msg.Version = com.V1
log.Printf("client: received message from Electron: %v", msg) log.Printf("client: received message from Electron: %v", msg)
encodedMsg, err := msg.Bytes() encodedMsg, err := msg.Bytes()
if err != nil { if err != nil {
@ -99,4 +108,3 @@ func readFromWebSocket(conn net.Conn, ws *websocket.Conn) {
conn.Write(encodedMsg) conn.Write(encodedMsg)
} }
} }