我正在使用 javascript 和 Play Framework 连接到 WebSocket 服务器。我只是无法理解使用 WebSocket 的 websocket.send() 方法的正确方法。
$(function() {
//depending on the browser we have to use WebSocket or MozWebSocket
var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket;
var websocket = new WS('ws://localhost:9000/webSocket');
websocket.onopen = function() {
websocket.send('test');
}
//split the message from the websocket into type, user and text
//uses a nifty regular expression
websocket.onmessage = function (event) {
$('.output').append(event.data);
}
});
当我send()在事件之外使用时,onopen我收到一个错误,因为send()不可用。我只是在想,必须有另一个事件来动态执行send(),而没有onopen(afaik)在连接打开时才执行的事件。
谢谢!