python - Websockets — Invalid UTF-8 bytes -
i'm trying build simple echoing server websockets, can connection stay alive few seconds before error. reason i'm getting websocket closing "invalid utf-8 bytes", i'm not sure invalid bytes coming from. here websocket client:
// websocket-client.es6 this.ws = new websocket(`ws://localhost:8080/websocket-test/ws`); this.ws.onopen = (event) => { this.ws.send('opening'); setinterval(() => { this.ws.send('heartbeat'); }, 5000); }; this.ws.onmessage = (event) => { console.log(event); }; this.ws.onclose = (event) => { console.log('websocket closing: %o', event); }; this.ws.onerror = (event) => { console.error('error: %o', event); }
my server cherrypy ws4py:
# websocket-server.py class mywebsocket(websocket): def received_message(self, message): cherrypy.log('received %s' % message) self.send(message.data, message.is_binary) def closed(self, code, reason=none): cherrypy.log('closed. code %s, reason: %s' % (code, reason))
when run app, on server side:
[10/feb/2016:16:39:42] received opening [10/feb/2016:16:39:47] received --heartbeat-- [10/feb/2016:16:39:52] received --heartbeat-- [10/feb/2016:16:39:57] received --heartbeat-- [10/feb/2016:16:40:02] received --heartbeat-- [10/feb/2016:16:40:07] received --heartbeat-- [10/feb/2016:16:40:12] received --heartbeat-- [10/feb/2016:16:40:17] received -helrtbhat-5 [10/feb/2016:16:40:22] received -helrtbhat-
the last 2 messages have open rectangle after second dash.
and chrome dev tools console says:
messageevent { data: "opening", type: "message" } websocket closed: closeevent { code: 1007, reason: "invalid utf-8 bytes", type: "close" }
where these invalid bytes coming from? looks i'm doing sending normal text. thanks.
in websocket-server.py
you have line:
self.send(message.data, message.is_binary)
so looks server thinks sending binary message chrome expecting text.
try:
self.send(message.data, message.is_text)
Comments
Post a Comment