节点js - Socket.io - 客户端未连接到socket.io服务器

时间:2018-04-19 05:52:09

标签: javascript node.js socket.io

我正在尝试连接到socket.io-client,使用它作为我的server.js

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

app.get('/input', function(req, res){ // This is because in the server the port 3001 is open and I only can use the route 'input' to connect to the node server
   res.send('Hello world');
});

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

   console.log('Client connected.');

   // Disconnect listener
   socket.on('disconnect', function() {
       console.log('Client disconnected.');
   });
});

在我的客户档案中:

var socket = io.connect('https://urlofthepage:3001', {reconnect: true});
socket.on('connect', function() { console.log('connect'); });

// or (I tried with both) 

var socket = io.connect('https://urlofthepage:3001/input', {reconnect: true});
socket.on('connect', function() { console.log('connect'); });

但是当我去urlofthepage/input告诉我

  

Hello world

但在节点服务器中显示类似

的内容
  

客户已连接。

在我有client.js文件的页面中,控制台显示我

  

3001 / socket.io /?EIO = 3& transport = polling& t = MBSSeZj net :: ERR_CONNECTION_TIMED_OUT

编辑:它位于已安装了WordPress的在线服务器上,我的socket.io.js脚本为urlofpage/socket.io/socket.io.js

AND(我不知道这是否重要,它是一个测试服务器,并且网址有https,但它表明它不安全,我们必须更改它)

0 个答案:

没有答案
相关问题