javascript - WebSocket with extra garbled in the end of message -
i want use websocket post mouse position device(iphone ...), after post post message anyoter device , can receieve message web ,but there strange thing ,it's have garbled code in end of message
first use prototype
create socket
, add function it
var socket = function(){ this.ws = new websocket("ws://172.16.3.103:60001"); this.ws.onopen = function() { console.log("[websocket#onopen]\n"); } this.ws.onmessage = function(e) { console.log("[websocket#onmessage] message: '" + e.data + "'\n"); } this.ws.onclose = function() { console.log("[websocket#onclose]\n"); self.ws = null ; } } socket.prototype.send = function(data){ console.log("send data server") if (this.ws) { var convert = json.stringify(data,null,4); var senddata = "50001#" + convert + "@" senddata = senddata.replace(/(\n)+|(\r\n)+/g, ""); console.log("send:",senddata) this.ws.send(senddata) this.ws.send("50001#*") } } socket.prototype.close = function(){ if (this.ws) { this.ws = null } } var socket = new socket()
then position canvas , use websocket send json message(i convert json jsonstring in send function)
this.canvas.addeventlistener('mousemove',function(event){ var x = event.pagex-getbodyoffsetleft(this), y = event.pagey-getbodyoffsettop(this); self.onmousemove({x:x,y:y}); var dict1 = {"type":"move","x":x,"y":y} var newdict = {"name":"line","dict":dict1} socket.send(newdict) },false);
after send message onmessage
callback running found strange thing in console ,most of message have garbled in end of message
[websocket#onmessage] message: '50001#{ "name": "line", "dict": { "type": "move", "x": 313, "y": 300 }}@�o[��n��-j��'
what happen?
Comments
Post a Comment