Javascript Websocket timeout - javascript

I'm working on small app using node.js and basic html/js web page.
On localhost, I have no problem, webSocket works as well, but when I try on my dedicated server (ovh/kimsufi server), my websocket client return timeout error :
On my server :
ws.js :
var http = require('http'),
WebSocket = require('ws'),
WebSocketServer = WebSocket.Server;
var server = http.createServer();
server.listen('3005', '0.0.0.0', function() {
console.log('Listening on ' + server.address().address + ':' + server.address().port);
var wss = new WebSocketServer({server: server});
wss.on('connection', function(client){
console.log('connected');
client.on('message', function(message) {
client.send('received');
console.log(data);
});
});
});
Result on Linux console :
-----#-----:~/---/---/nodeServer$ node ws.js Listening on 0.0.0.0:3005
On my client :
var p1 = new Promise(function(resolve, reject) {
ws = new WebSocket("ws://------.---:3005");
});
Result in chrome/firefox console :
comm.js:12 WebSocket connection to 'ws://------.---:3005/' failed:
Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
I think this error apears when I did a new virtualHost, can it block the websocket ?

Related

I'm getting a net::ERR_INSECURE_RESPONSE with socket.io/node.js

Here is the current code that I am using to try to connect my web application running socket.io to my node application.
var options = {
key: fs.readFileSync('csgo2x.key').toString(),
cert: fs.readFileSync('csgo2x.crt').toString(),
ca : [fs.readFileSync("csgo2x.pem").toString()],
rejectUnauthorized: false
};
var server = require('https').createServer(options);
var portNo = 443;
server.listen(portNo, function() {
console.log((new Date()) + " Server is listening on port " + portNo);
});
However...when I try to connect my socket.io application running on my Ubuntu server...it gives the error net::ERR_INSECURE_RESPONSE.
I am using certificates from Cloudflare btw and for some reason it's not working

client code in node js

I am new to Nodejs and am trying to set up a server client connection using sockets. Below is my code. Server is working OK but client is not connecting.
Please can anyone help me figure out the mistake.
Much Thanks
jessi
Server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
io.on('data', function(data) {
console.log('DATA from client is: ' + data);
// Close the client socket completely
});
server.listen(4200);
console.log('Monitoring server listening on port 4200');
Client.js
var HOST = '127.0.0.1';
var PORT = 4200;
var express = require('express');
var app = express();
var client = require('http').createServer(app);
var io = require('socket.io')(client);
client.connect(PORT, HOST, function()
{
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected,
// the server will receive it as message from the client
io.write('I am Chuck Norris!');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
console.log('DATA: ' + data);
// Close the client socket completely
client.destroy();
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});
For the client you use the socket.io-client package instead. The client side doesn't require the use of the Express portion since you're not recreating a web server on the client. If you look at your current code you're essentially recreating the Socket server which isn't what you want to do.
All that is necessary is to create a new Socket.io client and register your various event handlers.
var socket = require('socket.io-client')('localhost:4200');
socket.on('data', function(data) {
// handle incoming data
console.log(data);
});

Can't connect to wss:// (Error in connection establishment: net::ERR_CONNECTION_CLOSED)

I have VPS with LAMP. I have free signed SSL certificate from Startssl.com (ssl certificate working correctly)
With http:// protocol I can connect to ws://chat.example.com:1337/some-variable but when I replace protocol http:// to https:// then I can't connect to wss://chat.example.com:1337/some-variable
When I try to connect to wss:// I'm getting error Error in connection establishment: net::ERR_CONNECTION_CLOSED
Why?
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
"use strict";
// Optional. You will see this name in eg. 'ps' or 'top' command
process.title = 'node-chat';
// Port where we'll run the websocket server
var webSocketsServerPort = 1337;
// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');
var $ = require("jquery");
/**
* HTTP server
*/
var server = http.createServer(function(request, response) {
// Not important for us. We're writing WebSocket server, not HTTP server
});
server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});
/**
* WebSocket server
*/
var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});
EDIT
I have source from http://ahoj.io/nodejs-and-websocket-simple-chat-tutorial
To critique my answer better, as the last one was a mistake and I didn't mean to submit it until after I finished digging, try this:
// Private key and certification
var options = {
key: fs.readFileSync('cert/server.key'),
cert: fs.readFileSync('cert/server.crt')
};
var server = https.createServer(options, function(request, response) {
console.log((new Date()) + ' Received HTTP(S) request for ' + request.url);
}

WebSocket server not receiving messages immediately

The Node.JS server doesn't receive the message quickly..
Server code:
var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
var app = express()
var port = process.env.PORT || 5000
app.use(express.static(__dirname + "/"))
var server = http.createServer(app)
server.listen(port)
console.log("http server listening on %d", port)
var wss = new WebSocketServer({server: server})
console.log("websocket server created")
wss.on("connection", function(ws) {
var id = setInterval(function() {
ws.send(JSON.stringify(new Date()), function() { })
}, 1000)
console.log("websocket connection open")
ws.on("message", function(msg){
ws.send(msg);
})
ws.on("close", function() {
console.log("websocket connection close")
clearInterval(id)
})
})
Code to connect:
var z = new WebSocket("ws://appname.herokuapp.com/");
z.onopen = function(){ console.log("opened"); };
// ... after opened message seen in console, the following
// were typed directly into the console, a few seconds apart
z.send("H");
z.send("Hello World");
z.send("Hello World asdf");
Until the third send was done (even if it was a minute after the others), the server did not respond with H or Hello World; all three responses were made at the same time. How do I make the server respond in a timely manner?
(Using WireShark, I checked that the sends were actually done; they were)
Edit: The amount of requests/time before response varies each time.

WebSocket connection to OpenShift app failed

I created an app with NodeJS and I'm using ws module. If I test the app in localhost it works and there isn't any problem to connect websockets. Now I've upload the app to Openshift and when I try to access from the client it returns that is not possible to stablish a connection to the websocket.
If I do a tail in putty to my app I have this message: DEBUG: This type of response MUST NOT have a body. Ignoring data passed to end().
The code that I have in the server is:
#!/bin/env node
//Openshift variables
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "192.168.69.42";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
//NodeJS require modules
var Enum = require('enum');
var WebSocketServer = require('ws').Server
wss = new WebSocketServer({host:ipaddress, port:port});
var fs = require('fs');
wss.on('connection', function(ws) {
console.log((new Date()) + ' Connection from origin: ' + ws._socket.remoteAddress);
});
console.log((new Date()) + " Server is listening on: " + ipaddress + ':' port);
And in the client:
var ws = new WebSocket("ws://192.168.69.42:8080/");
ws.onopen = function() {
console.log("Connected.");
ws.send("This is the client speaking.");
};
For all WebSocket connections on OpenShift you need to use port 8000 (for Secured sessions it would be 8443). So, your server example works well (I run them after removing the unnecessary line var Enum = require('enum');, you just need to hardcode the port on client to 8000:
var ws = new WebSocket("ws://YourApp-YourName.rhcloud.com:8000");
ws.onopen = function(){
console.log('opened')
}
More information here.
Here is an example on github that works that you can check out: https://github.com/developercorey/openshift-nodejs-http-and-websocket-example

Categories

Resources