0

I am connecting to a WebSocket Server using javascript and the Play Framework. I am just having trouble understanding the proper way to make use of the websocket.send() method of the WebSocket.

 $(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);
    }
});

When I use send() outside the onopen event I get an error because send() is not available. I am just thinking that there must be another event to dynamically execute send() without the onopen event which (afaik) just executes when the connection opens up.

Thanks!

1 Answer 1

1

onopen is the correct event handler to use as it is the handler that is used when the connection is ready to be used.

the api docs show only 4 event handlers onopen onmessage onerror onclose

Api doc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.