Comparing version 0.0.5 to 0.0.6
@@ -13,3 +13,5 @@ /// <reference types="node" /> | ||
constructor(aClientName: string); | ||
readonly Connected: boolean; | ||
Connect: (aUrl: string) => Promise<void>; | ||
Disconnect: () => void; | ||
RequestDeviceList: () => Promise<void>; | ||
@@ -23,2 +25,3 @@ getDevices(): Device[]; | ||
ParseIncomingMessage: (aEvent: MessageEvent) => void; | ||
private onWebsocketClose; | ||
private SendMessage(aMsg); | ||
@@ -25,0 +28,0 @@ private SendMsgExpectOk; |
@@ -60,8 +60,9 @@ "use strict"; | ||
var _this = this; | ||
var res, rej, p; | ||
var ws, res, rej, p, conErrorCallback; | ||
return __generator(this, function (_a) { | ||
this._ws = new WebSocket(aUrl); | ||
this._ws.addEventListener("message", function (ev) { _this.ParseIncomingMessage(ev); }); | ||
ws = new WebSocket(aUrl); | ||
ws.addEventListener("message", function (ev) { _this.ParseIncomingMessage(ev); }); | ||
p = new Promise(function (resolve, reject) { res = resolve; rej = reject; }); | ||
this._ws.addEventListener("open", function (ev) { return __awaiter(_this, void 0, void 0, function () { | ||
conErrorCallback = function (ev) { rej(ev); }; | ||
ws.addEventListener("open", function (ev) { return __awaiter(_this, void 0, void 0, function () { | ||
var _this = this; | ||
@@ -71,7 +72,9 @@ var msg, ping; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.SendMessage(new Messages.RequestServerInfo(this._clientName))]; | ||
case 0: | ||
this._ws = ws; | ||
return [4 /*yield*/, this.SendMessage(new Messages.RequestServerInfo(this._clientName))]; | ||
case 1: | ||
msg = _a.sent(); | ||
switch (msg.getType()) { | ||
case "ServerInfo": | ||
case "ServerInfo": { | ||
ping = msg.MaxPingTime; | ||
@@ -83,7 +86,12 @@ if (ping > 0) { | ||
} | ||
this._ws.removeEventListener("close", conErrorCallback); | ||
this._ws.addEventListener("close", this.Disconnect); | ||
res(); | ||
break; | ||
case "Error": | ||
} | ||
case "Error": { | ||
this.Disconnect(); | ||
rej(); | ||
break; | ||
} | ||
} | ||
@@ -94,6 +102,17 @@ return [2 /*return*/]; | ||
}); }); | ||
this._ws.addEventListener("close", function (ev) { rej(ev); }); | ||
ws.addEventListener("close", conErrorCallback); | ||
return [2 /*return*/, p]; | ||
}); | ||
}); }; | ||
_this.Disconnect = function () { | ||
if (_this._pingTimer) { | ||
clearInterval(_this._pingTimer); | ||
} | ||
if (_this._ws === undefined) { | ||
return; | ||
} | ||
_this._ws.close(); | ||
_this._ws = undefined; | ||
_this.emit("close"); | ||
}; | ||
_this.RequestDeviceList = function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -104,3 +123,7 @@ var _this = this; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.SendMessage(new Messages.RequestDeviceList())]; | ||
case 0: | ||
if (this._ws === undefined) { | ||
throw new Error("ButtplugClient not connected"); | ||
} | ||
return [4 /*yield*/, this.SendMessage(new Messages.RequestDeviceList())]; | ||
case 1: | ||
@@ -190,2 +213,5 @@ deviceList = (_a.sent()); | ||
}; | ||
_this.onWebsocketClose = function (ev) { | ||
var a = 1; | ||
}; | ||
_this.SendMsgExpectOk = function (aMsg) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -214,3 +240,13 @@ var res, rej, msg, p; | ||
} | ||
Object.defineProperty(ButtplugClient.prototype, "Connected", { | ||
get: function () { | ||
return this._ws !== undefined; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ButtplugClient.prototype.getDevices = function () { | ||
if (this._ws === undefined) { | ||
throw new Error("ButtplugClient not connected"); | ||
} | ||
var devices = []; | ||
@@ -228,2 +264,5 @@ this._devices.forEach(function (d, i) { | ||
case 0: | ||
if (this._ws === undefined) { | ||
throw new Error("ButtplugClient not connected"); | ||
} | ||
dev = this._devices.get(aDevice.Index); | ||
@@ -249,2 +288,5 @@ if (dev === undefined) { | ||
case 0: | ||
if (this._ws === undefined) { | ||
throw new Error("ButtplugClient not connected"); | ||
} | ||
aMsg.Id = this._counter; | ||
@@ -251,0 +293,0 @@ msgPromise = new Promise(function (resolve) { res = resolve; }); |
@@ -53,4 +53,11 @@ "use strict"; | ||
return __generator(this, function (_a) { | ||
beforeEach(function () { | ||
beforeEach(function (done) { | ||
mockServer = new mock_socket_1.Server("ws://localhost:6868"); | ||
var serverInfo = function (jsonmsg) { | ||
var msg = Messages.FromJSON(jsonmsg)[0]; | ||
delaySend(new Messages.ServerInfo(0, 0, 0, 0, 0, "Test Server", msg.Id)); | ||
mockServer.removeEventListener("message", serverInfo); | ||
done(); | ||
}; | ||
mockServer.on("message", serverInfo); | ||
bp = new client_1.ButtplugClient("Test Buttplug Client"); | ||
@@ -57,0 +64,0 @@ bp.Connect("ws://localhost:6868"); |
{ | ||
"name": "buttplug", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Javascript library for accessing buttplug servers via node.js or web sockets", | ||
@@ -5,0 +5,0 @@ "main": "./dist/main/src/index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
882124
8764