html5 websocket 和长字符串
我有一个 websocket HTML5 页面,它从服务器接收数据,但奇怪的是,onmessage 事件似乎会针对长字符串多次触发。
w.onmessage = function(e) {
console.log ("\n\n message received... "+e.data);
}
如果我有一个 3000 个字符长的字符串,上面的代码将打印出前 2048 个字符,然后第二次打印剩下的内容。
无论如何,我是否可以检查类似(伪代码)(e.state == FINISHED
)或(e.state != UPDATING
)之类的东西,所以我在我拥有所有数据之前不要执行我的代码?现在,onmessage
函数在收到消息时会被多次调用,这会弄乱代码。
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.
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.
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? Right now, the onmessage
function is getting called multiple times when it receives a message and this is messing up the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在服务器端拆分字符串吗?在末尾添加一些内容以表明后面还有更多字符。
Can you split the string in the server side? Add some thing at the end to indicate that there are more characters behind.