colyseus.js
Advanced tools
Comparing version 0.6.0 to 0.7.0-alpha.1
@@ -1,9 +0,6 @@ | ||
import WebSocketClient from "websocket.js"; | ||
import { Signal } from "signals.js"; | ||
import { Room } from "./Room"; | ||
import { Signal } from "signals.js"; | ||
export declare class Client extends WebSocketClient { | ||
import { Connection } from "./Connection"; | ||
export declare class Client { | ||
id?: string; | ||
rooms: { | ||
[id: string]: Room<any>; | ||
}; | ||
onOpen: Signal; | ||
@@ -13,8 +10,8 @@ onMessage: Signal; | ||
onError: Signal; | ||
private _enqueuedCalls; | ||
constructor(url: string, protocols?: string[], options?: any); | ||
onOpenCallback(event: any): void; | ||
onCloseCallback(): void; | ||
onErrorCallback(): void; | ||
send(data: any): void; | ||
protected connection: Connection; | ||
protected room: Room; | ||
protected rooms: { | ||
[id: string]: Room; | ||
}; | ||
constructor(url: string); | ||
join<T>(roomName: string, options?: any): Room<T>; | ||
@@ -24,3 +21,3 @@ /** | ||
*/ | ||
onMessageCallback(event: any): void; | ||
protected onMessageCallback(event: any): void; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var websocket_js_1 = require("websocket.js"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var msgpack = require("msgpack-lite"); | ||
var signals_js_1 = require("signals.js"); | ||
var cookie = require("./Cookie"); | ||
var Protocol_1 = require("./Protocol"); | ||
var Room_1 = require("./Room"); | ||
var signals_js_1 = require("signals.js"); | ||
var Client = (function (_super) { | ||
__extends(Client, _super); | ||
function Client(url, protocols, options) { | ||
if (protocols === void 0) { protocols = null; } | ||
if (options === void 0) { options = {}; } | ||
var _this = _super.call(this, url, protocols, options) || this; | ||
_this.rooms = {}; | ||
var Connection_1 = require("./Connection"); | ||
var Client = (function () { | ||
function Client(url) { | ||
var _this = this; | ||
// signals | ||
_this.onOpen = new signals_js_1.Signal(); | ||
_this.onMessage = new signals_js_1.Signal(); | ||
_this.onClose = new signals_js_1.Signal(); | ||
_this.onError = new signals_js_1.Signal(); | ||
_this._enqueuedCalls = []; | ||
_this.binaryType = "arraybuffer"; | ||
return _this; | ||
this.onOpen = new signals_js_1.Signal(); | ||
this.onMessage = new signals_js_1.Signal(); | ||
this.onClose = new signals_js_1.Signal(); | ||
this.onError = new signals_js_1.Signal(); | ||
this.rooms = {}; | ||
var colyseusid = cookie.getItem('colyseusid'); | ||
if (colyseusid) { | ||
this.id = colyseusid; | ||
} | ||
this.connection = new Connection_1.Connection(url); | ||
this.connection.onmessage = this.onMessageCallback.bind(this); | ||
this.connection.onclose = function (e) { return _this.onClose.dispatch(); }; | ||
this.connection.onerror = function (e) { return _this.onError.dispatch(); }; | ||
} | ||
Client.prototype.onOpenCallback = function (event) { | ||
if (this._enqueuedCalls.length > 0) { | ||
for (var i = 0; i < this._enqueuedCalls.length; i++) { | ||
var _a = this._enqueuedCalls[i], method = _a[0], args = _a[1]; | ||
this[method].apply(this, args); | ||
} | ||
} | ||
}; | ||
Client.prototype.onCloseCallback = function () { | ||
this.onClose.dispatch(); | ||
}; | ||
Client.prototype.onErrorCallback = function () { | ||
this.onError.dispatch(); | ||
}; | ||
Client.prototype.send = function (data) { | ||
if (this.ws.readyState == WebSocket.OPEN) { | ||
return _super.prototype.send.call(this, msgpack.encode(data)); | ||
} | ||
else { | ||
// WebSocket not connected. | ||
// Enqueue data to be sent when readyState == OPEN | ||
this._enqueuedCalls.push(['send', [data]]); | ||
} | ||
}; | ||
Client.prototype.join = function (roomName, options) { | ||
if (options === void 0) { options = {}; } | ||
if (!this.rooms[roomName]) { | ||
this.rooms[roomName] = new Room_1.Room(this, roomName); | ||
} | ||
this.send([Protocol_1.Protocol.JOIN_ROOM, roomName, options]); | ||
return this.rooms[roomName]; | ||
this.room = new Room_1.Room(roomName); | ||
this.connection.send([Protocol_1.Protocol.JOIN_ROOM, roomName, options]); | ||
return this.room; | ||
}; | ||
@@ -64,48 +37,22 @@ /** | ||
Client.prototype.onMessageCallback = function (event) { | ||
var _this = this; | ||
var message = msgpack.decode(new Uint8Array(event.data)); | ||
var code = message[0]; | ||
var roomId = message[1]; | ||
if (code == Protocol_1.Protocol.USER_ID) { | ||
this.id = roomId; | ||
cookie.setItem('colyseusid', message[1]); | ||
this.id = message[1]; | ||
this.onOpen.dispatch(); | ||
} | ||
else if (code == Protocol_1.Protocol.JOIN_ROOM) { | ||
// joining room from room name: | ||
// when first room message is received, keep only roomId association on `rooms` object | ||
if (this.rooms[message[2]]) { | ||
this.rooms[roomId] = this.rooms[message[2]]; | ||
delete this.rooms[message[2]]; | ||
} | ||
this.rooms[roomId].id = roomId; | ||
this.rooms[roomId].onJoin.dispatch(); | ||
var room_1 = this.room; | ||
room_1.id = message[1]; | ||
room_1.connect(new Connection_1.Connection(this.connection.url + "/" + this.room.id)); | ||
room_1.onLeave.add(function () { return delete _this.rooms[room_1.id]; }); | ||
this.rooms[room_1.id] = room_1; | ||
} | ||
else if (code == Protocol_1.Protocol.JOIN_ERROR) { | ||
var room = this.rooms[roomId]; | ||
console.error("server error:", message[2]); | ||
if (room) { | ||
// room-related error | ||
room.onError.dispatch(message[2]); | ||
} | ||
else { | ||
// general error | ||
this.onError.dispatch(message[2]); | ||
} | ||
// general error | ||
this.onError.dispatch(message[2]); | ||
} | ||
else if (code == Protocol_1.Protocol.LEAVE_ROOM) { | ||
this.rooms[roomId].onLeave.dispatch(); | ||
} | ||
else if (code == Protocol_1.Protocol.ROOM_STATE) { | ||
var state = message[2]; | ||
var remoteCurrentTime = message[3]; | ||
var remoteElapsedTime = message[4]; | ||
this.rooms[roomId].setState(state, remoteCurrentTime, remoteElapsedTime); | ||
} | ||
else if (code == Protocol_1.Protocol.ROOM_STATE_PATCH) { | ||
var patches = message[2]; | ||
this.rooms[roomId].patch(patches); | ||
} | ||
else if (code == Protocol_1.Protocol.ROOM_DATA) { | ||
this.rooms[roomId].onData.dispatch(message[2]); | ||
this.onMessage.dispatch(message[2]); | ||
} | ||
else { | ||
@@ -116,3 +63,3 @@ this.onMessage.dispatch(message); | ||
return Client; | ||
}(websocket_js_1.default)); | ||
}()); | ||
exports.Client = Client; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Client_1 = require("./Client"); | ||
@@ -3,0 +4,0 @@ exports.Client = Client_1.Client; |
@@ -0,3 +1,4 @@ | ||
"use strict"; | ||
// Use codes between 0~127 for lesser throughput (1 byte) | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Protocol; | ||
@@ -4,0 +5,0 @@ (function (Protocol) { |
import { Signal } from "signals.js"; | ||
import Clock = require("clock.js"); | ||
import { DeltaContainer } from "delta-listener"; | ||
import { Client } from "./Client"; | ||
export declare class Room<T> { | ||
import { Connection } from "./Connection"; | ||
export declare class Room<T = any> { | ||
id: number; | ||
name: string; | ||
sessionId: string; | ||
state: DeltaContainer<T & any>; | ||
@@ -18,5 +19,7 @@ clock: Clock; | ||
private lastPatchTime; | ||
private client; | ||
connection: Connection; | ||
private _previousState; | ||
constructor(client: Client, name: string); | ||
constructor(name: string); | ||
connect(connection: Connection): void; | ||
protected onMessageCallback(event: any): void; | ||
setState(state: T, remoteCurrentTime?: number, remoteElapsedTime?: number): void; | ||
@@ -23,0 +26,0 @@ patch(binaryPatch: any): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var signals_js_1 = require("signals.js"); | ||
@@ -9,3 +10,3 @@ var Clock = require("clock.js"); | ||
var Room = (function () { | ||
function Room(client, name) { | ||
function Room(name) { | ||
var _this = this; | ||
@@ -30,6 +31,37 @@ this.state = new delta_listener_1.DeltaContainer({}); | ||
this.id = null; | ||
this.client = client; | ||
this.name = name; | ||
this.onLeave.add(this.removeAllListeners); | ||
} | ||
Room.prototype.connect = function (connection) { | ||
var _this = this; | ||
this.connection = connection; | ||
this.connection.onmessage = this.onMessageCallback.bind(this); | ||
this.connection.onclose = function (e) { return _this.onLeave.dispatch(); }; | ||
}; | ||
Room.prototype.onMessageCallback = function (event) { | ||
var message = msgpack.decode(new Uint8Array(event.data)); | ||
var code = message[0]; | ||
if (code == Protocol_1.Protocol.JOIN_ROOM) { | ||
this.sessionId = message[1]; | ||
this.onJoin.dispatch(); | ||
} | ||
else if (code == Protocol_1.Protocol.JOIN_ERROR) { | ||
this.onError.dispatch(message[2]); | ||
} | ||
else if (code == Protocol_1.Protocol.ROOM_STATE) { | ||
var state = message[2]; | ||
var remoteCurrentTime = message[3]; | ||
var remoteElapsedTime = message[4]; | ||
this.setState(state, remoteCurrentTime, remoteElapsedTime); | ||
} | ||
else if (code == Protocol_1.Protocol.ROOM_STATE_PATCH) { | ||
this.patch(message[2]); | ||
} | ||
else if (code == Protocol_1.Protocol.ROOM_DATA) { | ||
this.onData.dispatch(message[2]); | ||
} | ||
else if (code == Protocol_1.Protocol.LEAVE_ROOM) { | ||
this.leave(); | ||
} | ||
}; | ||
Room.prototype.setState = function (state, remoteCurrentTime, remoteElapsedTime) { | ||
@@ -63,8 +95,8 @@ this.state.set(state); | ||
Room.prototype.leave = function () { | ||
if (this.id >= 0) { | ||
this.client.send([Protocol_1.Protocol.LEAVE_ROOM, this.id]); | ||
if (this.id) { | ||
this.connection.close(); | ||
} | ||
}; | ||
Room.prototype.send = function (data) { | ||
this.client.send([Protocol_1.Protocol.ROOM_DATA, this.id, data]); | ||
this.connection.send([Protocol_1.Protocol.ROOM_DATA, this.id, data]); | ||
}; | ||
@@ -71,0 +103,0 @@ return Room; |
{ | ||
"name": "colyseus.js", | ||
"version": "0.6.0", | ||
"version": "0.7.0-alpha.1", | ||
"description": "Multiplayer Game Client for the Browser", | ||
@@ -18,3 +18,3 @@ "keywords": [ | ||
"scripts": { | ||
"start": "npm run build && nodemon --harmony ./examples/server.js", | ||
"start": "npm run build && node --harmony ./examples/server.js", | ||
"build-dist": "webpack --env.production", | ||
@@ -36,3 +36,3 @@ "build": "tsc && npm run build-dist", | ||
"tiny-emitter": "^1.1.0", | ||
"websocket.js": "^0.1.7" | ||
"websocket.js": "^0.1.10" | ||
}, | ||
@@ -50,3 +50,3 @@ "devDependencies": { | ||
"ts-loader": "^2.0.0", | ||
"typescript": "^2.1.4", | ||
"typescript": "^2.3.3", | ||
"uglify-js": "^2.6.1", | ||
@@ -53,0 +53,0 @@ "watchify": "^3.6.1", |
@@ -1,64 +0,40 @@ | ||
import WebSocketClient from "websocket.js"; | ||
import * as msgpack from "msgpack-lite"; | ||
import { Signal } from "signals.js"; | ||
import * as cookie from "./Cookie"; | ||
import { Protocol } from "./Protocol"; | ||
import { Room } from "./Room"; | ||
import { Connection } from "./Connection"; | ||
import { Signal } from "signals.js"; | ||
export class Client { | ||
public id?: string; | ||
export class Client extends WebSocketClient { | ||
id?: string; | ||
rooms: { [id: string]: Room<any> } = {}; | ||
// signals | ||
onOpen: Signal = new Signal(); | ||
onMessage: Signal = new Signal(); | ||
onClose: Signal = new Signal(); | ||
onError: Signal = new Signal(); | ||
public onOpen: Signal = new Signal(); | ||
public onMessage: Signal = new Signal(); | ||
public onClose: Signal = new Signal(); | ||
public onError: Signal = new Signal(); | ||
private _enqueuedCalls: any[] = []; | ||
protected connection: Connection; | ||
protected room: Room; | ||
protected rooms: {[id: string]: Room} = {}; | ||
constructor ( url: string, protocols: string[] = null, options: any = {} ) { | ||
super(url, protocols, options); | ||
this.binaryType = "arraybuffer"; | ||
} | ||
onOpenCallback (event) { | ||
if (this._enqueuedCalls.length > 0) { | ||
for (let i=0; i<this._enqueuedCalls.length; i++) { | ||
let [ method, args ] = this._enqueuedCalls[i]; | ||
this[ method ].apply(this, args); | ||
} | ||
constructor (url: string) { | ||
let colyseusid = cookie.getItem('colyseusid'); | ||
if (colyseusid) { | ||
this.id = colyseusid; | ||
} | ||
} | ||
onCloseCallback () { | ||
this.onClose.dispatch(); | ||
this.connection = new Connection(url); | ||
this.connection.onmessage = this.onMessageCallback.bind(this); | ||
this.connection.onclose = (e) => this.onClose.dispatch(); | ||
this.connection.onerror = (e) => this.onError.dispatch(); | ||
} | ||
onErrorCallback () { | ||
this.onError.dispatch(); | ||
} | ||
send (data: any): void { | ||
if (this.ws.readyState == WebSocket.OPEN) { | ||
return super.send( msgpack.encode(data) ) | ||
} else { | ||
// WebSocket not connected. | ||
// Enqueue data to be sent when readyState == OPEN | ||
this._enqueuedCalls.push(['send', [data]]) | ||
} | ||
} | ||
join<T> (roomName: string, options: any = {}): Room<T> { | ||
if (!this.rooms[ roomName ]) { | ||
this.rooms[ roomName ] = new Room<T>(this, roomName); | ||
} | ||
this.room = new Room<T>(roomName); | ||
this.send([Protocol.JOIN_ROOM, roomName, options]); | ||
this.connection.send([Protocol.JOIN_ROOM, roomName, options]); | ||
return this.rooms[ roomName ]; | ||
return this.room; | ||
} | ||
@@ -69,65 +45,27 @@ | ||
*/ | ||
onMessageCallback (event) { | ||
protected onMessageCallback (event) { | ||
let message = msgpack.decode( new Uint8Array(event.data) ); | ||
let code = message[0]; | ||
let roomId = message[1]; | ||
if (code == Protocol.USER_ID) { | ||
this.id = roomId | ||
cookie.setItem('colyseusid', message[1]); | ||
this.id = message[1]; | ||
this.onOpen.dispatch(); | ||
} else if (code == Protocol.JOIN_ROOM) { | ||
// joining room from room name: | ||
// when first room message is received, keep only roomId association on `rooms` object | ||
if (this.rooms[ message[2] ]) { | ||
this.rooms[ roomId ] = this.rooms[ message[2] ]; | ||
delete this.rooms[ message[2] ]; | ||
} | ||
let room = this.room; | ||
room.id = message[1]; | ||
room.connect(new Connection(`${ this.connection.url }/${ this.room.id }`)); | ||
room.onLeave.add(() => delete this.rooms[room.id]); | ||
this.rooms[ roomId ].id = roomId; | ||
this.rooms[ roomId ].onJoin.dispatch(); | ||
this.rooms[room.id] = room; | ||
} else if (code == Protocol.JOIN_ERROR) { | ||
let room = this.rooms[ roomId ]; | ||
console.error("server error:", message[2]); | ||
if (room) { | ||
// room-related error | ||
room.onError.dispatch(message[2]); | ||
// general error | ||
this.onError.dispatch(message[2]); | ||
} else { | ||
// general error | ||
this.onError.dispatch(message[2]); | ||
} | ||
} else if (code == Protocol.LEAVE_ROOM) { | ||
this.rooms[ roomId ].onLeave.dispatch(); | ||
} else if (code == Protocol.ROOM_STATE) { | ||
let state = message[2]; | ||
let remoteCurrentTime = message[3]; | ||
let remoteElapsedTime = message[4]; | ||
this.rooms[ roomId ].setState( state, remoteCurrentTime, remoteElapsedTime ); | ||
} else if (code == Protocol.ROOM_STATE_PATCH) { | ||
let patches = message[2]; | ||
this.rooms[ roomId ].patch( patches ); | ||
} else if (code == Protocol.ROOM_DATA) { | ||
this.rooms[ roomId ].onData.dispatch(message[2]); | ||
this.onMessage.dispatch(message[2]); | ||
} else { | ||
this.onMessage.dispatch(message); | ||
} | ||
@@ -134,0 +72,0 @@ |
@@ -10,6 +10,8 @@ import { Signal } from "signals.js"; | ||
import { Client } from "./Client"; | ||
import { Connection } from "./Connection"; | ||
export class Room<T> { | ||
export class Room<T=any> { | ||
public id: number; | ||
public name: string; | ||
public sessionId: string; | ||
@@ -31,13 +33,46 @@ public state: DeltaContainer<T & any> = new DeltaContainer<T & any>({}); | ||
private client: Client; | ||
public connection: Connection; | ||
private _previousState: any; | ||
constructor (client: Client, name: string) { | ||
constructor (name: string) { | ||
this.id = null; | ||
this.client = client; | ||
this.name = name; | ||
this.onLeave.add( this.removeAllListeners ); | ||
} | ||
connect (connection: Connection) { | ||
this.connection = connection; | ||
this.connection.onmessage = this.onMessageCallback.bind(this); | ||
this.connection.onclose = (e) => this.onLeave.dispatch(); | ||
} | ||
protected onMessageCallback (event) { | ||
let message = msgpack.decode( new Uint8Array(event.data) ); | ||
let code = message[0]; | ||
if (code == Protocol.JOIN_ROOM) { | ||
this.sessionId = message[1] | ||
this.onJoin.dispatch(); | ||
} else if (code == Protocol.JOIN_ERROR) { | ||
this.onError.dispatch(message[2]); | ||
} else if (code == Protocol.ROOM_STATE) { | ||
let state = message[2]; | ||
let remoteCurrentTime = message[3]; | ||
let remoteElapsedTime = message[4]; | ||
this.setState( state, remoteCurrentTime, remoteElapsedTime ); | ||
} else if (code == Protocol.ROOM_STATE_PATCH) { | ||
this.patch( message[2] ); | ||
} else if (code == Protocol.ROOM_DATA) { | ||
this.onData.dispatch(message[2]); | ||
} else if (code == Protocol.LEAVE_ROOM) { | ||
this.leave(); | ||
} | ||
} | ||
setState ( state: T, remoteCurrentTime?: number, remoteElapsedTime?: number ): void { | ||
@@ -82,4 +117,4 @@ this.state.set(state); | ||
public leave (): void { | ||
if (this.id >= 0) { | ||
this.client.send([ Protocol.LEAVE_ROOM, this.id ]); | ||
if (this.id) { | ||
this.connection.close(); | ||
} | ||
@@ -89,3 +124,3 @@ } | ||
public send (data): void { | ||
this.client.send([ Protocol.ROOM_DATA, this.id, data ]); | ||
this.connection.send([ Protocol.ROOM_DATA, this.id, data ]); | ||
} | ||
@@ -92,0 +127,0 @@ |
@@ -33,7 +33,7 @@ const webpack = require('webpack') | ||
}), | ||
new webpack.optimize.UglifyJsPlugin({ | ||
compress: {}, | ||
output: { comments: false }, | ||
sourceMap: false | ||
}) | ||
// new webpack.optimize.UglifyJsPlugin({ | ||
// compress: {}, | ||
// output: { comments: false }, | ||
// sourceMap: false | ||
// }) | ||
] | ||
@@ -40,0 +40,0 @@ : [] |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
944486
41
714
Updatedwebsocket.js@^0.1.10