-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.cpu.js
108 lines (107 loc) · 3.29 KB
/
socket.cpu.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
/**
CPU - socket
Dependency: core, util, auth
**/
(function() {
var modulename = "socket";
var Module = (function(modulename) {
function Module(options) {
this.name = modulename;
if (!options) {
console.log("Can't load module \"" + this.name + "\"! You need to pass some options.");
return;
}
if (!options["cpu"]) {
console.log("Can't load module \"" + this.name + "\"! You need to pass the cpu object.");
return;
}
this.cpu = options["cpu"];
if (!options["io"]) {
console.log("Can't load module \"" + this.name + "\"! You need to pass the io object.");
return;
}
var defaults = {
host : "localhost",
port : "8080",
protokoll : "http"
}
if(!options["server"]){
options["server"] = false;
}
this.io = options["io"];
this.server = options["server"];
if(!options["server"]) {
this.socket = this.io(defaults.protokoll + "://" + defaults.host + ":" + defaults.port);
}
this.names = [];
};
Module.prototype.registerSocket = function(socket, events){
var currentSocket = this.socket || socket;
if (!currentSocket) {
this.cpu.module("util").log("Socket not loaded!");
return;
}
var events = events || this.names;
for (var i = 0; i < events.length; i++) {
this._addEventtoSocket(events[i], this.cpu, currentSocket);
}
};
Module.prototype._addEventtoSocket = function(name,cpu, socket){
socket.on(name, function(data){
if (!this.server || cpu.module("auth").isLogin(socket.id) || name == "login") {
cpu.module("events").trigger("socket.receive." + name, { socket: socket, data: data });
} else {
cpu.module("events").trigger("socket.receive.nologin", {name: name, socket: socket, data: data});
}
});
};
Module.prototype.on = function(name, listeners){
name = name.trim();
listeners = listeners || {};
if (listeners["onemit"]) {
this.cpu.module("events").addEventListener("socket.emit." + name, listeners["onemit"]);
}
if (listeners["onreceive"]) {
this.names.push(name);
this.cpu.module("events").addEventListener("socket.receive." + name, listeners["onreceive"]);
}
};
Module.prototype.emit = function(name, data, socketID) {
var currentSocket;
if(socketID !== undefined){
currentSocket = this.io.to(socketID);
} else {
currentSocket = this.socket || undefined;
}
if (!currentSocket && !this.io.sockets) {
this.cpu.module("util").log("Socket not loaded!");
return;
}
name = name.trim();
data = data || {};
if(currentSocket === undefined) {
this.io.emit(name, data);
} else {
currentSocket.emit(name, data);
}
this.cpu.module("events").trigger("socket.emit." + name, { 'socket': currentSocket, 'data': data });
};
Module.prototype.trigger = function(name, data) {
if (!events[name]) {
events[name] = [];
}
for (var i = 0; i < events[name].length; i++) {
events[name][i](cpu, data);
}
};
return Module;
})(modulename);
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Module;
} else {
if (!window.cpumodules) {
window.cpumodules = {};
}
window.cpumodules[modulename] = Module;
}
})();