totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

WebSocketClient send JSON Got syntaxError #635

Closed ckpiggy closed 6 years ago

ckpiggy commented 6 years ago

When I trying to send JSON to client like

controller.all( (c) => {
    c.send({m: 'test'})
})

======= 2018-05-12 18:44:29: SyntaxError: Unexpected token o in JSON at position 1 SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at Framework.F.onParseJSON (/Users/ckpiggy/Documents/Projects/order_online/backend/server/node_modules/total.js/index.js:4991:14) at WebSocketClient.$decode (/Users/ckpiggy/Documents/Projects/order_online/backend/server/node_modules/total.js/websocketclient.js:442:44) at /Users/ckpiggy/Documents/Projects/order_online/backend/server/node_modules/total.js/websocketclient.js:485:27 at afterWrite (_stream_writable.js:454:3) at onwrite (_stream_writable.js:445:7) at InflateRaw.afterTransform (_stream_transform.js:90:3) at Zlib.callback (zlib.js:515:5)

2018-05-12 6 47 21

ckpiggy commented 6 years ago

I use

client.send(JSON.stringify({m: 'test'}))

It works

molda commented 6 years ago

Can you show me the way you defined the route? It should have the json flag otherwise it takes the data and turns it into a string which results into [object Object] The route with a json flag

F.websocket('/', wshandler, ['json']);

You can also stringify it yourself though

controller.all( (c) => {
    c.send(JSON.stringify({ m: 'test' }))
})
ckpiggy commented 6 years ago

A Ha! It works! Thank you @molda