Thanh Dao
Thanh Dao

Reputation: 1260

How to send parameters on declare WebSocket HTML5

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.

Upvotes: 1

Views: 805

Answers (1)

Jonny Whatshisface
Jonny Whatshisface

Reputation: 180

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...");
    };
}

Upvotes: 1

Related Questions