cflurin / homebridge-websocket

Homebridge Plugin
Apache License 2.0
40 stars 7 forks source link

Dies on exception `Unexpected token t in JSON` #5

Closed simonhac closed 7 years ago

simonhac commented 7 years ago

(apologies that i've had very little experience with WS, so there's every chance that i'm doing something stupid.)

i'm using the Simple WebSocket Client to communicate with homebridge-websocket. after connecting (successfully) to ws://127.0.0.1:4050, i send {topic: "get", payload: {"name": "all"}}.

the homebridge instance bails with the exception:

[5/1/2017, 8:34:12 PM] [websocket] client ip ::ffff:127.0.0.1 connected
  cbus:client rx event { time: '20170501-203419.950', code: 700, processed: false, message: 'cgate - Heartbeat.', type: 'event', raw: '#e# 20170501-203419.950 700 cgate - Heartbeat.' } +10s
  cbus:platform Heartbeat @ 20170501-203419.950 +1ms
undefined:1
{topic: "get", payload: {"name": "all"}}
 ^

SyntaxError: Unexpected token t in JSON at position 1
    at Object.parse (native)
    at Websocket.onMessage (/Users/simon/.nvm/versions/node/v6.10.2/lib/node_modules/homebridge-websocket/lib/websocket.js:71:18)
    at Websocket.message (/Users/simon/.nvm/versions/node/v6.10.2/lib/node_modules/homebridge-websocket/lib/websocket.js:51:12)
    at emitTwo (events.js:106:13)
    at WebSocket.emit (events.js:191:7)
    at Receiver.ontext (/Users/simon/.nvm/versions/node/v6.10.2/lib/node_modules/homebridge-websocket/node_modules/ws/lib/WebSocket.js:816:10)
    at /Users/simon/.nvm/versions/node/v6.10.2/lib/node_modules/homebridge-websocket/node_modules/ws/lib/Receiver.js:477:18
    at /Users/simon/.nvm/versions/node/v6.10.2/lib/node_modules/homebridge-websocket/node_modules/ws/lib/Receiver.js:361:7
    at /Users/simon/.nvm/versions/node/v6.10.2/lib/node_modules/homebridge-websocket/node_modules/ws/lib/PerMessageDeflate.js:238:5
    at afterWrite (_stream_writable.js:388:3)

any advice welcome.

simonhac commented 7 years ago

same also happens when sending from Dark WebSocket Terminal.

(i'm just entering {topic: "get", payload: {"name": "all"}} -- nothing else.)

cflurin commented 7 years ago

try using apostrophes:

{"topic": "get", "payload": {"name": "all"}}
simonhac commented 7 years ago

thanks @cflurin -- there were no apostrophes in your sample comment. can you please clarify?

cflurin commented 7 years ago

Does it work with apostrophes? I tested homebridge-websocket with node-red, node-red adds apostrophes automatically when missing. If it works with apostrophes, I'll revise README.

simonhac commented 7 years ago

sorry, i don't know what you mean by apostrophes -- i know the ' symbol, but where do you intend for it to go? around the text or instead of the quotes or somewhere else?

cflurin commented 7 years ago

sorry, I mean quotes (" symbol) around "topic" and "payload":

{"topic": "get", "payload": {"name": "all"}}
simonhac commented 7 years ago

thanks @cflurin -- that works: image

can you suggest a more interesting query that shows off an interesting data stream? (i'll spend some more time over the next couple of days familiarising myself with WS and homebridge-websocket.)

can i suggest that it might be a good idea to harden homebridge-websocket against bad input?

cflurin commented 7 years ago
simonhac commented 7 years ago

thanks @cflurin -- i really appreciate your help. (i'll check out homebridge-mqtt and homebridge-mvc.)