mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-05 07:57:00 +08:00
feat: implement websocket and UI
This commit is contained in:
32
dag/websocket.go
Normal file
32
dag/websocket.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package dag
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/oarkflow/mq/sio"
|
||||
)
|
||||
|
||||
func WsEvents(s *sio.Server) {
|
||||
s.On("join", join)
|
||||
s.On("message", message)
|
||||
}
|
||||
|
||||
func join(s *sio.Socket, data []byte) {
|
||||
//just one room at a time for the simple example
|
||||
currentRooms := s.GetRooms()
|
||||
for _, room := range currentRooms {
|
||||
s.Leave(room)
|
||||
}
|
||||
s.Join(string(data))
|
||||
s.Emit("joinedRoom", string(data))
|
||||
}
|
||||
|
||||
type msg struct {
|
||||
Room string
|
||||
Message string
|
||||
}
|
||||
|
||||
func message(s *sio.Socket, data []byte) {
|
||||
var m msg
|
||||
json.Unmarshal(data, &m)
|
||||
s.ToRoom(m.Room, "message", m.Message)
|
||||
}
|
Reference in New Issue
Block a user