简体   繁体   English

html5 websockets 和长字符串

[英]html5 websockets and long strings

I have a websocket HTML5 page that receives data from the server, but curiously the onmessage event seems to fire multiple times for long strings.我有一个从服务器接收数据的 websocket HTML5 页面,但奇怪的是onmessage事件似乎对长字符串多次触发。

w.onmessage = function(e) {
  console.log ("\n\n message received... "+e.data);
}

If I have a string 3000 characters long, the above code will print out the first 2048 characters and then whatever is left over on the second time around.如果我有一个 3000 个字符长的字符串,上面的代码将打印出前 2048 个字符,然后是第二次剩下的任何内容。

Is there anyway for me to check for something like (pseudo-code) ( e.state == FINISHED ) or ( e.state != UPDATING ) or something so I don't execute my code until I have all of the data?无论如何,我是否可以检查诸如(伪代码)( e.state == FINISHED )或( e.state != UPDATING )之类的东西,以便在获得所有数据之前我不会执行我的代码? Right now, the onmessage function is getting called multiple times when it receives a message and this is messing up the code.现在, onmessage函数在收到消息时会被多次调用,这弄乱了代码。

Can you split the string in the server side?您可以在服务器端拆分字符串吗? Add some thing at the end to indicate that there are more characters behind.在末尾添加一些东西以表示后面还有更多字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM