1

I want to send params in the first connect to socket server.
I'm looking for a sending method like:

var wsUrl = 'ws://localhost:9300',
wsHandle = new WebSocket(wsUrl, [myParam]);

or like:

var wsUrl = 'ws://localhost:9300',
wsHandle = new WebSocket(wsUrl, {
    myKey: myParam
});

Anyone help me with some keywords? Thanks for your answers.

1 Answer 1

1

I'm assuming you mean you want to send something upon connecting?

var socket;

function OpenSocket() {
    if (window.hasOwnProperty("WebSocket")) { // webkit-browser
        socket = new WebSocket("ws://localhost:12345/");
    }
    else if (window.hasOwnProperty("MozWebSocket")) { // firefox
        socket = new MozWebSocket("ws://localhost:12345/");
    }
    else { // browser doesnt support websockets
        alert("Your browser doesn't support WebSocket.");
        return;
    }

    socket.onopen = function() { // the socket is ready, send something
        //socket.send("Testing websocket data flow...");
    };
}
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.