mirror of
https://github.com/raz-varren/sacrificial-socket.git
synced 2025-10-06 08:36:56 +08:00
45 lines
860 B
Go
45 lines
860 B
Go
/*
|
|
A simple web chat app example that does not implement any multihome backends.
|
|
*/
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
ss "github.com/raz-varren/sacrificial-socket"
|
|
"github.com/raz-varren/log"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
s := ss.NewServer()
|
|
|
|
s.On("join", join)
|
|
s.On("message", message)
|
|
|
|
http.Handle("/socket", s.WebHandler())
|
|
http.Handle("/", http.FileServer(http.Dir("webroot")))
|
|
|
|
log.Err.Fatalln(http.ListenAndServe(":80", nil))
|
|
}
|
|
|
|
func join(s *ss.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 *ss.Socket, data []byte) {
|
|
var m msg
|
|
json.Unmarshal(data, &m)
|
|
s.Roomcast(m.Room, "message", m.Message)
|
|
}
|