-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.js
170 lines (138 loc) · 4.39 KB
/
socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// bind and events dependencies
var Bind = require("github/jillix/bind");
var Events = require("github/jillix/events");
/**
* Web Sockets
* Mono module implementation for web sockets.
*
* */
module.exports = function init (config) {
// create the socket
var socket = io.connect(config.origin || ("//" + location.host));
// get self (the module)
var self = this;
// store config in self.config
self.config = config;
// process config
processConfig.call(self);
// run the binds
for (var i = 0; i < config.binds.length; ++i) {
Bind.call(self, config.binds[i]);
}
/**
* self.socketInit (object, function);
* Inits the socket in the page. This is called automatically!
*
* options: an object containing the following fields:
* - force: if the websocket is already inited and `force` is `true`,
* the websocket will be reinited
* - log: log level passed to socket.io
*
* callback: a function that is called when the socket it inited
*
* */
self.socketInit = function (options, callback) {
// call the server operation
self.link("init", {data: options}, function (err, data) {
// first time when initing the websocket
if (!err && data === "REFRESH") {
location.reload();
}
callback (err, data);
});
};
/**
* self.clientEmit (object, function);
* Emits an event and data to the server
*
* options: an object containing
* - event: the event name
* - data: the data that goes to server side
*
* callback: a function that is passed too to the emit function
* from socket emit function
* */
self.clientEmit = function (options, callback) {
// default value for callback
callback = callback || function () {};
// validate options
if (!options || options.constructor !== Object && options.constructor !== String) {
throw new Error ("Options must be a string or an object");
}
// validate callback
if (!callback || callback.constructor !== Function) {
throw new Error ("Callback must be a function");
}
// emit event passing options and callback
self.socket.emit(options._event || options.event || options, options, callback);
}
/**
* self.clientListen (object, function);
* Listen an event that comes from the server
*
* options: an object containing
* - event: the event name
*
* callback: a function that is passed too to the on function
*
* */
self.clientListen = function (options, callback) {
// listen for events from server
self.socket.on(options.event, callback);
};
/**
* self.serverSend (options);
* Call the sendMessage from the server
*
* message: an object containing
* - type (string): client, session, group, or all
* - session (session id): which clinet should recive this message; all if undefined
* - data (object)
* */
self.serverSend = function (options) {
// override the event field
options._event = "sockets.server.send";
// emit a server event
self.clientEmit(options);
};
/**
* self.serverEmitGlobal (options);
* Emits a global event using M.emit()
*
* options:
* - event: the event name that is emited
* - data: data to be emited
* */
self.serverEmitGlobal = function (options) {
// override the event field
options._event = "sockets.server.emitGlobal";
// global server event
self.clientEmit(options);
};
/**
* This automatically inits the socket in the page
*
* */
self.socketInit({}, function (err) {
// handle error
if (err) { return self.emit("error", err); }
// set socket object
self.socket = socket;
// emit ready
self.emit("ready", self.config, socket);
});
// call events
Events.call(self, config);
};
/**
* Process config
*
* */
function processConfig () {
// get self (the module)
var self = this;
// binds
self.config.binds = self.config.binds || [];
// `options` is an object
self.config.options = self.config.options || {};
}