Documentation ¶
Index ¶
- Variables
- func DispatcherJs(w http.ResponseWriter, r *http.Request)
- type BroadcastMessage
- type Conn
- type ConnCloseHandler
- type Dispatcher
- func (d *Dispatcher) Broadcast(room string, message interface{}) error
- func (d *Dispatcher) Close(ConnectionID string) error
- func (d *Dispatcher) EmitTo(recipient string, event string, msg interface{}) error
- func (d *Dispatcher) Handle(w http.ResponseWriter, r *http.Request, h *http.Header) (*Conn, error)
- type DispatcherConfig
- type Handler
- type MessageData
Constants ¶
This section is empty.
Variables ¶
View Source
var ClientJs = []byte(`var Dispatcher = (function () {
var conf = {
onJoin: null,
onLeve: null,
handlers: {},
roomHandlers: {},
conn: null
};
function Dispatcher(endpoint) {
conf.conn = new WebSocket(endpoint);
conf.conn.onmessage = function (msg) {
var data = JSON.parse(msg.data);
if (data.s === true) {
if (data.e == "broadcast") {
if (conf.roomHandlers.hasOwnProperty(data.m.r)) {
conf.roomHandlers[data.m.r](data.m.m);
}
}
} else {
if (conf.handlers.hasOwnProperty(data.e)) {
conf.handlers[data.e](data.m);
}
}
};
}
Dispatcher.prototype.OnConnect = function (cb) {
conf.conn.onopen = cb;
};
Dispatcher.prototype.OnDisconnect = function (cb) {
conf.conn.onclose = cb;
};
Dispatcher.prototype.OnJoin = function (cb) {
conf.onJoin = cb;
};
Dispatcher.prototype.OnLeve = function (cb) {
conf.onLeve = cb;
};
Dispatcher.prototype.Emit = function (event, message) {
msg = {
e: event,
s: false,
m: message
};
conf.conn.send(JSON.stringify(msg));
};
Dispatcher.prototype.EmitTo = function (recipient, event, message) {
msg = {
e: "emitTo",
s: true,
m: {
e: event,
r: recipient,
m: message
}
};
conf.conn.send(JSON.stringify(msg));
};
Dispatcher.prototype.Broadcast = function (room, message) {
msg = {
e: "broadcast",
s: true,
m: {
r: room,
m: message
}
};
conf.conn.send(JSON.stringify(msg));
};
Dispatcher.prototype.On = function (event, cb) {
conf.handlers[event] = cb;
};
Dispatcher.prototype.Join = function (room, cb) {
conf.roomHandlers[room] = cb;
msg = {
e: "join",
s: true,
m: room
};
conf.conn.send(JSON.stringify(msg));
};
Dispatcher.prototype.Leave = function (room) {
msg = {
e: "leave",
s: true,
m: room
};
conf.conn.send(JSON.stringify(msg));
};
Dispatcher.prototype.Disconnect = function () {
conf.conn.close();
};
return Dispatcher;
} ());`)
Functions ¶
func DispatcherJs ¶
func DispatcherJs(w http.ResponseWriter, r *http.Request)
DispatcherJs handles client js code requests
Types ¶
type BroadcastMessage ¶
type BroadcastMessage struct { Room string `json:"r"` Message interface{} `json:"m"` }
type Conn ¶
type Conn struct { ID string // contains filtered or unexported fields }
func (*Conn) OnClose ¶
func (c *Conn) OnClose(h ConnCloseHandler)
OnClose subscribe to connection close
type ConnCloseHandler ¶
type ConnCloseHandler func(*Conn)
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
func NewDispatcher ¶
func NewDispatcher(config *DispatcherConfig) *Dispatcher
NewDispatcher create dispatcher
func (*Dispatcher) Broadcast ¶
func (d *Dispatcher) Broadcast(room string, message interface{}) error
Broadcast send message to a room subscribers
type DispatcherConfig ¶
type MessageData ¶
Click to show internal directories.
Click to hide internal directories.