2

I have a simple node.js code for websocket server using socket.io module and I just cannot make it work. The server runs with no errors but when trying to connect from the browser I am getting "SOCKET ERROR: {"isTrusted":true}" error (I am not using self signed certificate but Symantec certificate which is tested and is trusted by browsers).
An here is my code:

var fs = require( 'fs' ) ;
var app = require('express')();
var https  = require('https');
var server = https.createServer({    
    key: fs.readFileSync('cert.key'),
    cert: fs.readFileSync('cert.cer'),
},app);
server.listen(50100);
var io = require('socket.io').listen(server,{pingTimeout: 7000, pingInterval: 10000});
io.sockets.on('connection',function (socket) {
    console.log('new connection'); 
});

On client I am using standard java script "var WS = new WebSocket(wsUri); " and wsUri is "wss://siteaddress:50100".
I tried the code with "ws" node module and it seems to work fine but "ws" module doesnt have all the methods I need (unless I am missing something - I need to be able to send messages to selected sockets or all sockets together and send some info from client on socket connection).
I have tried all the suggestions in node.js, socket.io with SSL but none of them worked for me.
Tried debugging by DEBUG=* node myapp - here what I get in the log:

express:application set "x-powered-by" to true +0ms
express:application set "etag" to 'weak' +7ms
express:application set "etag fn" to [Function: wetag] +2ms
express:application set "env" to 'development' +1ms
express:application set "query parser" to 'extended' +0ms
express:application set "query parser fn" to [Function: parseExtendedQueryString] +1ms
express:application set "subdomain offset" to 2 +0ms
express:application set "trust proxy" to false +1ms
express:application set "trust proxy fn" to [Function: trustNone] +1ms
express:application booting in development mode +2ms
express:application set "view" to [Function: View] +1ms
express:application set "views" to '/opt/bitnami/apache2/wm/node/views' +0ms
express:application set "jsonp callback name" to 'callback' +1ms
socket.io:server initializing namespace / +145ms
socket.io-parser encoding packet {"type":0,"nsp":"/"} +1ms
socket.io-parser encoded {"type":0,"nsp":"/"} as 0 +1ms
socket.io:server creating engine.io instance with opts {"pingTimeout":7000,"pingInterval":10000,"path":"/socket.io","initialPacket":["0"]} +0ms
socket.io:server attaching client serving req handler +10ms

Nothing appears in the log when client tries to connect to the websocket.
Can anybody point me in the right direction?
Thanks.

user3195616
  • 195
  • 2
  • 13

2 Answers2

0

Try adding the transport and origins with the corresponding port

After var io = require('socket.io').listen(server ....)

Add this:

io.set('transports', ['websocket']); io.set('origins', 'YOUR_ORIGIN:*');

Check that your rules are correctly set on apache or nginx in order that you have wss.

perseus
  • 1,322
  • 1
  • 16
  • 22
-1

server side:

`var express = require('express');
var app = express();
var server = app.listen(8080);
var io = require('socket.io').listen(server);`

client side:

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>

var mySocket = io.connect('localhost:8080', {transports: 
 ['websocket'], reconnection: false,secure:false}); 

mySocket.on('connect', function () {
    mySocket.sendBuffer=[]
    console.log('Connected!');
});  

if your browser loged connected means connection is established

farhadamjady
  • 982
  • 6
  • 14
  • On your code I am getting "WebSocket connection to 'ws://mysite:8080/socket.io/?EIO=3&transport=websocket' failed: Connection closed before receiving a handshake response". I noticed "secure" is set to false - should not it be true if I want to use SSL (I tried it with the same result). And I believe since last few versions Firefox does not allow unsecure websocket connection. Thanks – user3195616 Jun 12 '17 at 21:02