Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 25, 2022 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save percybolmer/66c279f375b6c8d03a6776706611424f to your computer and use it in GitHub Desktop.
Save percybolmer/66c279f375b6c8d03a6776706611424f to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"time"
)
// Event is the Messages sent over the websocket
// Used to differ between different actions
type Event struct {
// Type is the message type sent
Type string `json:"type"`
// Payload is the data Based on the Type
Payload json.RawMessage `json:"payload"`
}
// EventHandler is a function signature that is used to affect messages on the socket and triggered
// depending on the type
type EventHandler func(event Event, c *Client) error
const (
// EventSendMessage is the event name for new chat messages sent
EventSendMessage = "send_message"
// EventNewMessage is a response to send_message
EventNewMessage = "new_message"
// EventChangeRoom is event when switching rooms
EventChangeRoom = "change_room"
)
// SendMessageEvent is the payload sent in the
// send_message event
type SendMessageEvent struct {
Message string `json:"message"`
From string `json:"from"`
}
// NewMessageEvent is returned when responding to send_message
type NewMessageEvent struct {
SendMessageEvent
Sent time.Time `json:"sent"`
}
// SendMessageHandler will send out a message to all other participants in the chat
func SendMessageHandler(event Event, c *Client) error {
// Marshal Payload into wanted format
var chatevent SendMessageEvent
if err := json.Unmarshal(event.Payload, &chatevent); err != nil {
return fmt.Errorf("bad payload in request: %v", err)
}
// Prepare an Outgoing Message to others
var broadMessage NewMessageEvent
broadMessage.Sent = time.Now()
broadMessage.Message = chatevent.Message
broadMessage.From = chatevent.From
data, err := json.Marshal(broadMessage)
if err != nil {
return fmt.Errorf("failed to marshal broadcast message: %v", err)
}
// Place payload into an Event
var outgoingEvent Event
outgoingEvent.Payload = data
outgoingEvent.Type = EventNewMessage
// Broadcast to all other Clients
for client := range c.manager.clients {
// Only send to clients inside the same chatroom
if client.chatroom == c.chatroom {
client.egress <- outgoingEvent
}
}
return nil
}
type ChangeRoomEvent struct {
Name string `json:"name"`
}
// ChatRoomHandler will handle switching of chatrooms between clients
func ChatRoomHandler(event Event, c *Client) error {
// Marshal Payload into wanted format
var changeRoomEvent ChangeRoomEvent
if err := json.Unmarshal(event.Payload, &changeRoomEvent); err != nil {
return fmt.Errorf("bad payload in request: %v", err)
}
// Add Client to chat room
c.chatroom = changeRoomEvent.Name
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment