I use express and socket.io
My node server is running on 127.0.0.1:3000
and i have connected it
var socket = io.connect('http://127.0.0.1:3000', {query: 'id=' + user.id});
There is a warning in my browser console.
WebSocket connection to 'ws://127.0.0.1:3000/socket.io/?id=ecad7e1a009eb4f943ead0b4db8d62c4&EIO=3&transport=websocket&sid=0RMaRoZb7U67td_2AAAD' failed: Invalid frame header
Also it runs well. There is no problem :). But i wondered.
Maybe you should check versions of socket client and server. They should be the same.
Related
I have an app running behind web server. Initially user loads a page via http (not https, because my http server is running on router and I need to avoid certificate warnings), then when page is loaded a websocket connection is open (wss) to the app that provides data for the web page.
It works fine, if no SSL used - http (no SSL) server + ws (no SSL) websocket.
It works fine, if I use public domain name (just for testing, not on router) with SSL (https) + wss (SSL) websocket.
But it does not work (both on local domain name or public) if SSL certificate is self-signed for websocket (not matter, http or https is used).
Tested with 2048 and 4096 bits keys.
The following code is used to connect:
try {
// var socket = new WebSocket(this.url); // This does not work, I believe because of self-signed certificate is rejected
var socket = new WebSocket(this.url, {
// protocolVersion: 8, // Tried but no difference
// origin: 'https://myserver.com:443', // Tried but no difference
rejectUnauthorized: false // This line produces an error below
})
socket.onclose = function(event) {
console.log("closed:", event.code, event.reason); // code is `1006`
})
socket.onerror = function(error) {
console.log("error:", error); // Nothing useful in this error object
})
socket.onmessage = function(event) {
console.log("data:", event.data);
})
} catch(e) {
this.error("connect error reason: " + e) // Is never called
}
Firefox error:
error: connect error reason: SyntaxError: An invalid or illegal string was specified
Chrome error:
error: connect error reason: SyntaxError: Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid.
I am able to connect to wss server using wscat (with -n option to ignore self-signed certificate error):
wscat -n -c wss://myserver.com:8443
How do I connect from browser JS to SSL websocket server with self-signed certificate?
P.S. Please keep in mind, that I will use plain http to load the web page.
UPDATE: I found this question with answer that states that parameters must be passed differently to websocket costructor:
var socket = new WebSocket(this.url, null, null, null, {rejectUnauthorized: false});
But I don't see this constructor in docs.
Though, I tried the fix and if http is used, I still get 1006 error in Chrome, but I get 1015 (TLS handshake reserved).
When using the same self-signed SSL certificate for https server and try to load the page, I get 1006 error both in Chrome and Firefox.
On the local server, these settings work
CHAT_WS_SERVER_HOST = '0.0.0.0'
CHAT_WS_SERVER_PORT = 3000
CHAT_WS_SERVER_PROTOCOL = 'ws'
And the chat is working fine, but on Heroku there is an error(Chrome):
WebSocket connection to 'ws://0.0.0.0:41540/jgvnf53ky1m1lxt1vrka9lcnfilwhkrv/Pechkin' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
On Heroku I use such settings:
CHAT_WS_SERVER_HOST = '0.0.0.0'
CHAT_WS_SERVER_PORT = os.environ['PORT'];
CHAT_WS_SERVER_PROTOCOL = 'ws'
Python code
websocket = new WebSocket(base_ws_server_path + '{{ request.session.session_key }}/' + opponent_username);
JS code on client
websocket = new WebSocket(base_ws_server_path + 'jgvnf53ky1m1lxt1vrka9lcnfilwhkrv/' + opponent_username);
Fihish
ws://0.0.0.0:41540/jgvnf53ky1m1lxt1vrka9lcnfilwhkrv/Pechkin
I read that this is a problem because of the certificate (wss \ https), but I don't use him now... or with the wrong IP
Upd_1
The IP address 0.0.0.0 refers to the local server. When your browser connects to that address, it will reach the computer where the browser is running (e.g. your laptop). Since you want it to connect to your Heroku app, you need to replace the value 0.0.0.0 with the hostname for your Heroku app, e.g. your-app-name.herokuapp.com. This way, the browser will connect over the internet to your Heroku app instead.
The URL in the browser should look like ws://your-app-name.herokuapp.com:...
I have been developing a live chat that uses a websocket connection. It works fine on my localhost and on http servers. However, I am trying to get it working on a secure GoDaddy server with Deluxe Linux Hosting.
While the client is trying to open a websocket connection from javascript, it eventually responds with "WebSocket connection to 'wss://jhaubrich.com/new.projecthandle.io/Demo/include/projectChat/php-socket.php?username=Justin' failed: Error during WebSocket handshake: Unexpected response code: 504"
A 504 Gateway Timeout Error indicates an issue with the gateway or proxy server.
If I try to open the connection again without killing the PID for the lsphp, the error written to error_log by the php-socket.php file reads "PHP Warning: socket_bind(): unable to bind address [98]: Address already in use in /home/fq2cvob2t06k/public_html/new.projecthandle.io/Demo/include/projectChat/php-socket.php on line 12"
For the socket_bind() in php-socket.php I am using port 8090.
I have tried changing ports. I have tried adding 'websocket' to the end of the url in the websocket js.
//my js to initiate the websocket
var websocket = new WebSocket("ws://jhaubrich.com/new.projecthandle.io/Demo/include/projectChat/php-socket.php?username=" + userName + "" );
I expected a websocket connection to be made.
Instead I get this error : "WebSocket connection to 'wss://jhaubrich.com/new.projecthandle.io/Demo/include/projectChat/php-socket.php?username=Justin' failed: Error during WebSocket handshake: Unexpected response code: 504"
I came to the realization that I am use a shared hosting environment. I need to purchase a Virtual Private Server where I will have root privileges. Then I will be able to use WebSockets...
I am new to WebRTC and WebSockets and was following this tutorial to create a WebRTC demo project, but I am unable to create a WebSocket connection. I have followed the same steps as mentioned in the project.
His project is running on port 8080 and he mentioned ws://localhost:9090. My project is running on port 8081, but I copied his URL ws://localhost:9090 because I didn't know the significance of 9090 and I received this error and my server is node.js. i changed local host to 8081 as well but then i am getting hand shake error.
WebSocket connection to 'ws://localhost:9090/' failed: Error in
connection establishment: net::ERR_CONNECTION_REFUSED.
Chrome doesn't allow unsecure websocket (ws) connections to localhost (only wss, so you should setup a TLS certificate for your local web/websocket server).
However the same should work fine with Firefox.
You need to use ws://yourIp:9090/, where yourIP is like 192.168.?.?.
Usually WebRTC requires a secure connection (that is https).
The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project.
Provide a valid TLS/SSL certificate and also configure it correctly in project, then it will work without the above error.
try to change the port to 8080
const ws = new WebSocket('ws://localhost:8080/chat')
I guess this is a generic websocket issue.
Change the url to a dynamic name using the built-in location.host variable and change the protocol to secure websocket wss if you have set-up the TLS:
const ws = new WebSocket("wss://" + location.host + "/")
Port 9090 is used by reactotron. Probably you are using it in your project and your app cannot connect with reactotron because it is closed. Just open reactotron and the error will disappear.
also you could easily change the mappings of IP addresses to host names,
on windows go to C:\Windows\System32\drivers\etc\hosts and uncomment this line
127.0.0.1 localhost
save and restart.
WebSocket connection to 'ws://localhost:8080/' failed
Must ensure server file is running
I git this above problem
maybe you forgot to start websocket server, check it again, with configuration in my project, run:
php artisan websocket:init
[I've flagged this question to be deleted by moderator until I get answer to my this question]
My javascript client is working fine when talking to node.js(on Ubuntu, more here) server through WebSocket interface with IPV4 IP address.
Even though my Hosting provider is ready to allocate me IPV4, let me instead use IPV6 since there is limited supply of IPV4 now.
When using the IPV6 IP I'm getting this error:
WebSocket connection to 'ws://[2600:3c00::f03c:91ff:fe73:2b08]:31333/'
failed: Error in connection establishment:
net::ERR_ADDRESS_UNREACHABLE
I've checked that port 31333 is indeed open using tool: http://www.subnetonline.com/pages/ipv6-network-tools/online-ipv6-port-scanner.php
I've even tried solution as in IPv6 address giveing syntax error in internet explorer-10 websocket -- that is : 2600-3c00-0-0-f03c-91ff-fe73-2b08.ipv6-literal.net
I've run this client and is able to connect to the server when both are running on remote Ubuntu based server:
var WebSocket = require('ws')
, ws = new WebSocket('ws://[2600:3c00::f03c:91ff:fe73:2b08]:31333');
ws.on('open', function() {
ws.send('something');
});
ws.on('message', function(message) {
console.log('received: %s', message);
});
However when I run this same client from my home Windows 7 while the server is remote Ubuntu one, it is not able to connect:
> $ node c2.js events.js:85
> throw er; // Unhandled 'error' event
> ^ Error: connect ENETUNREACH
> at exports._errnoException (util.js:746:11)
> at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
I've even run this code in Google Chrome :
var ws = new WebSocket("ws://[2600:3c00::f03c:91ff:fe73:2b08]:31333");
ws.onerror = function(err)
{
console.log(err);
alert("Connection is error...");
};
But I get the same error:
WebSocket connection to 'ws://[2600:3c00::f03c:91ff:fe73:2b08]:31333/'
failed: Error in connection establishment:
net::ERR_ADDRESS_UNREACHABLE websocket.html:37 Event
What could be the issue?
PS: at the moment I've stopped the server at port 31333.
IPv6 is not enabled on your Realtek PCIe GBE Family Controller. If it was, there would be a line saying "Link-local IPv6 Address" in the ipconfig result.
Go to control panel > Network > Change Adapter Settings > Right click on the adapter > Properties. Check IPv6 on.
IPv6 will need to be enabled on the system, and every router between it and the ISP, and the ISP must be IPv6 ready and providing it to the end-user. The reason nslookup works is that it is just returning AAAA records, not actually connecting via IPv6. IPv4 cannot directly communicate with IPv6, or vice versa. You can't ping an IPv6 host with an IPv4 host.