2017-12-02 11:49:27 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2017-12-10 19:39:02 +01:00
|
|
|
this.connections = [];
|
|
|
|
|
2017-12-02 11:49:27 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-10 19:39:02 +01:00
|
|
|
GameLib.SocketsRuntime.prototype.connect = function(serverIp, port, protocol) {
|
|
|
|
var connection = new WebSocket('ws://' + serverIp + ':' + port , protocol);
|
|
|
|
this.connections.push(connection);
|
|
|
|
};
|
|
|
|
|
2017-12-02 11:49:27 +01:00
|
|
|
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() {
|
2017-12-04 21:04:24 +01:00
|
|
|
if (this.instance !== WebSocket) {
|
2017-12-02 11:49:27 +01:00
|
|
|
console.error('Only WebSocket supported');
|
|
|
|
throw new Error('Only WebSocket supported');
|
|
|
|
}
|
|
|
|
};
|