1
votes

I am new to socket.io and node.js I refered some online documents and created a socket server, it was working fine, but now it shows so many errors, Since I don't know socket.io and node.js I am unable to rectify the problem.

I am receiving the following error in my server side

/home/sitename/public_html/chat-server/node_modules/socket.io/lib/namespace.js:119 fns[i](socket, function(err){ ^ TypeError: Property '0' of object [object Object] is not a function at run (/home/sitename/public_html/chat-server/node_modules/socket.io/lib/namespace.js:119:11) at Namespace.run (/home/sitename/public_html/chat-server/node_modules/socket.io/lib/namespace.js:131:3) at Namespace.add (/home/sitename/public_html/chat-server/node_modules/socket.io/lib/namespace.js:160:8) at Client.connect (/home/sitename/public_html/chat-server/node_modules/socket.io/lib/client.js:76:20) at Server.onconnection (/home/sitename/public_html/chat-server/node_modules/socket.io/lib/index.js:367:10) at Server.EventEmitter.emit (events.js:95:17) at Server.handshake (/home/sitename/public_html/chat-server/node_modules/engine.io/lib/server.js:310:8) at Server.onWebSocket (/home/sitename/public_html/chat-server/node_modules/engine.io/lib/server.js:392:10) at /home/sitename/public_html/chat-server/node_modules/engine.io/lib/server.js:335:12 at completeHybiUpgrade2 (/home/sitename/public_html/chat-server/node_modules/ws/lib/WebSocketServer.js:284:5)

and in the client side I am receiving the following error

GET https://sitename/socket.io/?EIO=3&transport=polling&t=1483766845342-50

and

WebSocket connection to 'wss://sitename:3000/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

client side code

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

  var socket = io();

    var socket = io.connect( 'https://sitename:3000',{secure: true,
'sync disconnect on unload': true,'reconnect': true,
  'reconnection delay': 500, rejectUnauthorized: false ,   transports: [
    'websocket', 
    'flashsocket', 
    'htmlfile', 
    'xhr-polling', 
    'jsonp-polling', 
    'polling'
  ]} );

server side code

var fs = require('fs');
var app = require('express')();
var https = require('https');
var options = {
  key: fs.readFileSync('./privatekey.pem'),
  cert: fs.readFileSync('./certificate.crt')
};
var server = https.createServer(options, app);
//var io = require('socket.io')(server);
var io = require('socket.io').listen(server);
var socket = io.use({
  transports: [
    'websocket', 
    'flashsocket', 
    'htmlfile', 
    'xhr-polling', 
    'jsonp-polling', 
    'polling'
  ]
});



var clients=[];
var gamename={};
var socketid={};


app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'https://sitename:3000');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});


app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){

});

server.listen(3000, function(){

  console.log('It is Listening on *:3000');
});
2
I would hazard a guess that one of your transports is not valid. You could try removing them all except for websocket and see if you learn anything.jfriend00
@jfriend00 Thank you for your time. Do I have to add it in server side or client side ?scriptkiddie1
@jfriend00 still the same problem in the serverscriptkiddie1
I'd say remove all transports entirely from both client and server so you have a default implementation (which should just be websocket and xhr-polling) and see if the problem is still there. Why are you adding so many transports? And, what are the htmlfile and polling transports? You can't just make up transport names that are not supported in your implementation.jfriend00
@jfriend00 thanks bro, I will try it.scriptkiddie1

2 Answers

0
votes

It looks to me like you probably have invalid transports specified which causes socket.io to make an error (I don't recognize transport names like htmlfile and polling).

The way you know if this is the case or not is to remove all transports entirely from both client and server so you don't even specify that option. Then you will have only the default, built-in transports (which should just be just websocket and xhr-polling).

You must only specify transports that you have support for and that you actually need. You cannot just make up transport names.

0
votes
let jwt = require('jsonwebtoken');

You should pass object "{ transports: [] }" into .listen func as below:

let io = require('socket.io').listen(server, {
    transports: ['websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']
});

into use() you can pass cb func as below:

io.use(async (socket, next) => {
    const {
        handshake: {
            query
        }
    } = socket;
    if (query && .query.token){
        jwt.verify(query.token, 'SECRET_KEY', function(err, decoded){
          if(err) return next(new Error('Authentication error'));
          socket.decoded = decoded;
          next();
       });
    } else {
      next(new Error('Authentication error'));
    }    
})
 .on('connection', (socket) => {
    // emit ot listen socket
    socket.on('message', (message) => {
       console.log('=================== ', message);
 })
});