Reputation: 7316
I am using jquery to make a WebSocket handshake request. Here is the code
$.ajax({
url: 'http://'+ document.location.host + '/handshake',
type: 'GET',
dataType: 'json',
headers: {
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Upgrade', 'websocket');
xhr.setRequestHeader('Connection', 'Upgrade');
xhr.setRequestHeader('Sec-WebSocket-Key', 'dGhlIHNhbXBsZSBub25jZQ==');
xhr.setRequestHeader('Sec-WebSocket-Version', '13');
},
success: function (result) {
// CallBack(result);
},
error: function (error) {
}
});
I tried setting headers within headers
section also, but I still get the below error and headers are not set in the handshake request.
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers says that handshake is the very first request that client(browser) makes
Upvotes: 1
Views: 695
Reputation: 26
AJAX post error : Refused to set unsafe header "Connection" seems to be related.
I believe if you want to make a WebSocket handshake upgrade request, you can use the WebSocket
object native to Javascript to do what you need (it sends the request, implicitly setting headers):
var webSocket = new WebSocket('ws://localhost:8080/yourURL');
webSocket.onerror = function(event) {
//Handle the error event, ('error' in your code)
};
webSocket.onopen = function(event) {
//Handle what happens when the connection is made ('success' in your code)
};
webSocket.onmessage = function(event) {
//What do do on each message
};
With your code, you are simply not allowed to touch those headers.
Upvotes: 1