From 9479b2b87ac8fb0117caed4c65f761fdcaa90f05 Mon Sep 17 00:00:00 2001 From: nakamurataichi Date: Thu, 2 Feb 2017 18:05:34 +0900 Subject: [PATCH 1/3] changed to socket.io --- js/sphero-client.js | 47 +++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/js/sphero-client.js b/js/sphero-client.js index 72bc9c1..546a62d 100644 --- a/js/sphero-client.js +++ b/js/sphero-client.js @@ -1,68 +1,61 @@ import eventPublisher from "./publisher"; -import Sphero from "sphero-client"; +import socketIOClient from "socket.io-client"; class SpheroClient { constructor() { this.clientKey = null; - this.orb = null; + this.socket = null; } connect(wsHost) { - if (this.orb !== null) return; - this.orb = new Sphero(); - this.orb.connect(wsHost, () => { + if (this.socket !== null) return; + this.socket = new socketIOClient(wsHost); + this.socket.on("connect", () => { eventPublisher.publish("ws-connected"); - eventPublisher.subscribe("spheroState", spheroState => { - if (spheroState === "idling") { - this.orb.finishCalibration(); - } else if (spheroState === "calibrating") { - this.orb.startCalibration(); - } - }); eventPublisher.subscribe("currentCommands", commands => { - this.orb.sendCustomMessage("commands", commands); + this.socket.emit("commands", commands); }); }, () => { eventPublisher.publish("ws-error"); }); - this.orb.listenCustomMessage("hp", hp => { + this.socket.on("hp", hp => { eventPublisher.publish("hp", hp); }); - this.orb.listenCustomMessage("gameState", gameState => { + this.socket.on("gameState", gameState => { eventPublisher.publish("gameState", gameState); }); - this.orb.listenCustomMessage("rankingState", rankingState => { + this.socket.on("rankingState", rankingState => { eventPublisher.publish("rankingState", rankingState); }); - this.orb.listenCustomMessage("ranking", playerState => { + this.socket.on("ranking", playerState => { eventPublisher.publish("playerState", playerState); }); - this.orb.listenCustomMessage("availableCommandsCount", count => { + this.socket.on("availableCommandsCount", count => { eventPublisher.publish("availableCommandsCount", count); }); - this.orb.listenCustomMessage("oni", isOni => { + this.socket.on("oni", isOni => { eventPublisher.publish("oni", isOni); }); - this.orb.listenCustomMessage("clientKey", key => { + this.socket.on("clientKey", key => { this.clientKey = key; }); - this.orb.listenCustomMessage("acceptName", name => { + this.socket.on("acceptName", name => { eventPublisher.publish("acceptName", name); }); - this.orb.listenCustomMessage("rejectName", () => { + this.socket.on("rejectName", () => { eventPublisher.publish("rejectName", name); }); - this.orb.listenCustomMessage("color", color => { + this.socket.on("color", color => { eventPublisher.publish("color", color); }); } requestName(name) { - if (this.orb !== null) { - this.orb.sendCustomMessage("requestName", name); + if (this.socket !== null) { + this.socket.emit("requestName", name); } } useDefinedName(name) { - if (this.orb !== null) { - this.orb.sendCustomMessage("useDefinedName", name); + if (this.socket !== null) { + this.socket.emit("useDefinedName", name); } } } From 54acd04cc6a96cd044c2ac47aa9811cc5094e777 Mon Sep 17 00:00:00 2001 From: nakamurataichi Date: Thu, 2 Feb 2017 21:36:36 +0900 Subject: [PATCH 2/3] fixed initial --- js/sphero-client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/sphero-client.js b/js/sphero-client.js index 546a62d..ce294a5 100644 --- a/js/sphero-client.js +++ b/js/sphero-client.js @@ -1,5 +1,5 @@ import eventPublisher from "./publisher"; -import socketIOClient from "socket.io-client"; +import SocketIOClient from "socket.io-client"; class SpheroClient { constructor() { @@ -8,7 +8,7 @@ class SpheroClient { } connect(wsHost) { if (this.socket !== null) return; - this.socket = new socketIOClient(wsHost); + this.socket = new SocketIOClient(wsHost); this.socket.on("connect", () => { eventPublisher.publish("ws-connected"); eventPublisher.subscribe("currentCommands", commands => { @@ -41,7 +41,7 @@ class SpheroClient { this.socket.on("acceptName", name => { eventPublisher.publish("acceptName", name); }); - this.socket.on("rejectName", () => { + this.socket.on("rejectName", name => { eventPublisher.publish("rejectName", name); }); this.socket.on("color", color => { From ab9ace3a56574410272490af6ebdeca244a99412 Mon Sep 17 00:00:00 2001 From: nakamurataichi Date: Sat, 4 Feb 2017 17:42:03 +0900 Subject: [PATCH 3/3] added log message --- js/sphero-client.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/js/sphero-client.js b/js/sphero-client.js index ce294a5..27c6d82 100644 --- a/js/sphero-client.js +++ b/js/sphero-client.js @@ -4,10 +4,11 @@ import SocketIOClient from "socket.io-client"; class SpheroClient { constructor() { this.clientKey = null; - this.socket = null; } connect(wsHost) { - if (this.socket !== null) return; + console.log(`connect server... ${wsHost}`); + + if (!this.socket) return; this.socket = new SocketIOClient(wsHost); this.socket.on("connect", () => { eventPublisher.publish("ws-connected"); @@ -49,12 +50,12 @@ class SpheroClient { }); } requestName(name) { - if (this.socket !== null) { + if (!this.socket) { this.socket.emit("requestName", name); } } useDefinedName(name) { - if (this.socket !== null) { + if (!this.socket) { this.socket.emit("useDefinedName", name); } }