67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
/**
|
|
* Sockets
|
|
* @param id
|
|
* @param name
|
|
* @param socketsType
|
|
* @constructor
|
|
*/
|
|
GameLib.SocketsRuntime = function(
|
|
id,
|
|
name,
|
|
socketsType
|
|
) {
|
|
if (GameLib.Utils.UndefinedOrNull(id)) {
|
|
id = GameLib.Utils.RandomId();
|
|
}
|
|
this.id = id;
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(name)) {
|
|
name = 'Sockets (' + id + ')';
|
|
}
|
|
this.name = name;
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(socketsType)) {
|
|
socketsType = GameLib.SocketsRuntime.TYPE_WEB_SOCKET;
|
|
}
|
|
this.socketsType = socketsType;
|
|
|
|
this.connections = [];
|
|
|
|
this.createInstance();
|
|
};
|
|
|
|
/**
|
|
* GameLib.SocketsRuntime Types
|
|
* @type {number}
|
|
*/
|
|
GameLib.SocketsRuntime.TYPE_WEB_SOCKET = 0x1;
|
|
|
|
GameLib.SocketsRuntime.prototype.createInstance = function() {
|
|
if (this.socketsType === GameLib.SocketsRuntime.TYPE_WEB_SOCKET) {
|
|
this.instance = WebSocket;
|
|
} else {
|
|
this.instance = null;
|
|
}
|
|
};
|
|
|
|
GameLib.SocketsRuntime.prototype.connect = function(server) {
|
|
var connection = new WebSocket(server.protocol + '://' + server.ip + ':' + server.port , server.protocols);
|
|
this.connections.push(connection);
|
|
};
|
|
|
|
GameLib.SocketsRuntime.prototype.updateInstance = function(property) {
|
|
if (property === 'socketsType') {
|
|
this.createInstance();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Logs a warning and throws an error if not cannon
|
|
*/
|
|
GameLib.SocketsRuntime.prototype.isNotWebSocketThrow = function() {
|
|
if (this.instance !== WebSocket) {
|
|
console.error('Only WebSocket supported');
|
|
throw new Error('Only WebSocket supported');
|
|
}
|
|
};
|