Web Socket handshake error in Docker - javascript

I use Python 3.5, Flask and Meinheld web socket middleware to serve sockets. Here is server side code sample:
#app.route('/chat')
def chat():
ws = request.environ.get('wsgi.websocket')
while True:
m = ws.wait()
if m is None:
break
ws.send(m)
return ""
Client side code sample:
var s = new WebSocket("ws://localhost:4567/chat");
However, everything works properly while I run it in local mode, but when i add this into docker container i always get error like this:
WebSocket connection to 'ws://localhost:4567/chat' failed: Error during WebSocket handshake: Unexpected response code: 500
It does not matter where docker container is run, on remote server or local environment, the error is always the same. All necessary ports are exposed.

Related

How to connect from browser JS to websocket server with self-signed SSL certificate?

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.

Websocket : Error during WebSocket handshake: Unexpected response code: 504

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...

AWS websocket server: 403 error when connecting from browser, fine from Python

I deployed a websocket server on an AWS EC2 instance. I am able to connect to this websocket without any issues from a Python script (using websocket.create_connection), but I get a 403 error when running the Javascript line var ws = new Websocket(.....) in Chrome: Error during WebSocket handshake: Unexpected response code: 403.
I made sure to change the security group on the EC2 instance to allow all inbound and outbound traffic.
All suggestions appreciated!
EDIT: Figured out what was going on here. It was an issue with the default origin check function in my Go websocket server.
This was a server-side issue with the gorilla/websocket package. Had to add this CheckOrigin function:
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {return true},
}

Tomcat in Docker with webapp: Error during WebSocket handshake

I have a Java webapp and a browser application that connects to it over WebSockets like this in my client-side Javascript code:
function connect() {
var username = 'my-user';
var url = `ws://${document.location.host}${document.location.pathname}chat/${username}`;
log(`making WebSocket connection to ${url}`)
var ws = new WebSocket(url);
ws.onmessage = (event) => {
var message = JSON.parse(event.data);
log(message.message);
};
ws.onerror = (event) => {
log(event);
}
}
The endpoint when running Tomcat locally is ws://localhost:8080/my-app/chat/my-user
The endpoint when running Tomcat under Docker is ws://localhost:8090/my-app/chat/my-user (port 8090)
This works fine when connecting to my local Tomcat, but not when connecting to Docker Tomcat.
The error I get back when trying to connect to the Docker Tomcat is:
WebSocket connection to 'ws://localhost:8090/my-app/chat/my-user' failed: Error during WebSocket handshake: Unexpected response code: 404
Any ideas? From what I can tell, I don't need to open any additional ports in Docker (I have 8090 mapped properly). I don't know if there is any additional logging I can turn on in Tomcat to show me the endpoints. Maybe I just am not specifying the right endpoint?

MQTT 1884 connection failed

I am using mqtt for a messaging system on my site. Well on my local machine everything works fine but on my site (production) I am getting an error:
vendor.bbaf8c4….bundle.js:1 WebSocket connection to
'ws://localhost:1884/mqtt' failed: Error in connection establishment:
net::ERR_CONNECTION_REFUSED
I am using Ubuntu within my Webserver and I gave the port 1884 free.
What this error meaning?
I am using Angular4 and I add the connection within AfterViewInit
ngAfterViewInit() {
setTimeout(()=>{
this.messageservice.createMqttConnection(this.userId);
}, 200)
}
The connection with Paho on my client site looks like:
this._client = new Paho.MQTT.Client("localhost", 1884, id.toString());
Let assume my domains is mydomain.com should I replace it with localhost?
As thrashed out in the comments:
The hostname in your app needs to point to your domain e.g. mydomain.com not localhost
localhost will always point to the machine the code is running on so is very unlikely to be what you want for a production deployment of an application.

Categories

Resources