Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
biohackerellie committed Dec 6, 2024
1 parent 44c28fb commit 20916b2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release-go.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Go Server Only Build
on:
workflow_dispatch:
push:
branches: ["main"]
schedule:
- cron: '0 2 * * *'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand Down
46 changes: 20 additions & 26 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -79,51 +80,44 @@ func (c *Client) readPump() {
// Handle normal WebSocket close events
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
log.Printf("WebSocket closed by client: %v", c.conn.RemoteAddr())
} else if strings.Contains(err.Error(), "connection reset by peer") {
// Suppress log for expected errors
log.Printf("Connection reset by peer for client: %v. Cleaning up.", c.conn.RemoteAddr())
} else {
log.Printf("Error reading message from client: %v, err: %v", c.conn.RemoteAddr(), err)
}
return // Exit on read error
return // Exit on any read error
}

// Handle valid messages
switch messageType {
case websocket.TextMessage: // JSON or plain text
case websocket.TextMessage:
// Process text messages
var message map[string]interface{}
if err := json.Unmarshal(rawMessage, &message); err != nil {
log.Printf("Invalid JSON from client: %v, message: %s, err: %v", c.conn.RemoteAddr(), string(rawMessage), err)
continue // Skip invalid JSON messages
continue // Skip invalid JSON
}

action, _ := message["action"].(string)
channel, _ := message["channel"].(string)

if action == "" || channel == "" {
continue
}

switch action {
case "ping":
if err := c.conn.WriteMessage(websocket.TextMessage, []byte(`{"action":"pong"}`)); err != nil {
log.Printf("Error sending pong to client: %v, err: %v", c.conn.RemoteAddr(), err)
return
}

case "subscribe":
c.hub.register <- &Subscription{Client: c, Channel: channel}

case "unsubscribe":
c.hub.unregister <- &Subscription{Client: c, Channel: channel}

case "message":
payload := message["payload"]
msg := Message{
Channel: channel,
Event: "message",
Payload: payload,
}
c.hub.broadcast <- msg

default:
log.Printf("Unhandled action: %s", action)
}

case websocket.PingMessage:
// Reply to WebSocket ping frames
if err := c.conn.WriteMessage(websocket.PongMessage, nil); err != nil {
log.Printf("Error responding to ping from client: %v, err: %v", c.conn.RemoteAddr(), err)
return
}

default:
log.Printf("Unsupported message type from client: %v, type: %d", c.conn.RemoteAddr(), messageType)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ts-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class SocketClient {
if (this.heartbeatTimer) clearInterval(this.heartbeatTimer);
this.heartbeatTimer = setInterval(() => {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.send({ action: 'ping' });
this.send('');
}
}, this.heartbeatInterval);

Expand Down

0 comments on commit 20916b2

Please sign in to comment.