从socket.io

时间:2016-12-11 19:51:25

标签: javascript node.js sockets express socket.io

我知道之前发布过类似的问题。但我已经阅读并尝试了所有提出的解决方案,但我仍然没有让它发挥作用。

我正在尝试使用socket.io连接两个设备。但每次我收到此错误:

  

GET http://localhost:4444/socket.io/?EIO=3&transport=polling&t=LZkz-Kk 404(找不到档案)polling-xhr.js:261

     

Request.create @ polling-xhr.js:261
  要求@ polling-xhr.js:166
  XHR.request @ polling-xhr.js:93
  XHR.doPoll @ polling-xhr.js:123
  Polling.poll @ polling.js:118
  Polling.doOpen @ polling.js:63
  Transport.open @ transport.js:80
  Socket.open @ socket.js:240
  Socket @ socket.js:119
  Socket @ socket.js:29
  Manager.open.Manager.connect @ manager.js:213
  经理@ manager.js:68
  经理@ manager.js:37
  lookup @ index.js:60
  socketSetup @ sketch.js:9
  init @ sketch.js:69

然后总是会出现这个错误,但我想这是一个问题,因为它无法找到我的文件,对吧?

  

获取http://localhost:4444/socket.io/?EIO=3&transport=polling&t=LZk-142 net :: ERR_CONNECTION_REFUSED polling-xhr.js:261

我已尝试过各种不同的方式来SRC我在线遇到的socket.io文件,这些是一些例子:

< script type="text/javascript" src="http://localhost:4444/socket.io/socket.io.js">< /script ><br>
< script type="text/javascript" src="http://myipadress:4444/socket.io/socket.io.js">< /script ><br>
< script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.2/socket.io.js">< /script ><br>
< script type="text/javascript" src="/socket.io/socket.io.js">< /script><br>
< script src="libraries/socket.io.js">< /script><br>
< script type="text/javascript" src="/node_modules/socket.io-client/dist/socket.io.js">< /script>

基本上,他们都给我同样的错误,除非我离开SRC,因为有人说不应该隐含套接字链接。在那种情况下,我得到:

  

未捕获的ReferenceError:未定义io(...)sketch.js:9   socketSetup @ sketch.js:9
  init @ sketch.js:69

这是我的服务器端, app.js:

var express = require('express');<br>
var app = express();<br>
var PORT = 4444;<br>
// Routing<br>
app.use('/', express.static(__dirname + '/public'));<br>
// Socket.io setup<br>
var server = require('http').createServer(app);<br>
var io = require('socket.io')(server);<br>
server.listen(PORT, function(){<br>
    console.log('Server listening at port ' + PORT);<br>
});<br>
<br>
io.on('connection', function(socket) {<br>
A bunch of my io code.<br>
)};

这是我的index.html代码:

< !DOCTYPE html><br>
< html><br>
< head><br>
  < meta charset="UTF-8"><br>
    < link rel="stylesheet" href="css/style.css"><br>
< /head><br>
<br>
< body><br>
<br>
< script type="text/javascript" src="http://localhost:4444/socket.io/socket.io.js">< /script><br>
< !-- < script type="text/javascript" src="/socket.io/socket.io.js">< /script>-- ><br>
< script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/p5.js">< /script><br>
< script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/addons/p5.dom.js">< /script><br>
< script type="text/javascript" src="libraries/jquery-min.js">< /script><br>
< script type="text/javascript" src="sketch.js">< /script><br>
< /body><br>
< /html><br>

的package.json

{
  "name": "public",
  "version": "1.0.0",
  "description": "Test",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "kim",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0",
    "socket.io": "^1.7.2"
  }
}

这是我的客户方的重要部分, sketch.js:

var app = app || {};

app.main = (function() {

var socket;

var socketSetup = function(callback){
    console.log('Called socketStart.');
      socket = io.connect();
      socket.on('movement', function(data) {
          Socket listeners..
        }
      );  
      callback();    
  } 
var init = function(){
    console.log('Initializing app.');
    //var myp5 = new p5(sketch, 'canvas-container');
    socketSetup(sketch);
  };

  return {
    init: init
  };

})();

window.addEventListener('DOMContentLoaded', app.main.init);

我尝试了一些不同的设置,但这在逻辑上应该有效吗?^^

最后,这是我的项目结构:

app.js
package.json
node_modules
public
sketch.js
index.html
css
libraries

我甚至尝试在公共文件夹中本地安装socket.io,但这也不起作用。

有人能看出我做错了什么吗?我有没有尝试过我应该做的事情?什么对你有用?

对于这么长的帖子感到抱歉,我只是想清楚一下我试过的以避免不必要的答案。我甚至尝试卸载并删除所有节点和npm文件并重新安装。

1 个答案:

答案 0 :(得分:0)

您可能希望从这个简单的设置开始:

服务器:(您的代码几乎没有变化)

var express = require('express');
var app = express();
var PORT = 4444;
// Routing
app.use('/', express.static(__dirname + '/public'));
// Socket.io setup
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(PORT, function(){
console.log('Server listening at port ' + PORT);
});

io.on('connection', function(socket) {
    console.log('a client has connected');
});

/public/index.html

<!doctype html>
<html>
  <head>
    <title>Testing socket.io</title>
  </head>
  <body>
    <h1 id="socketio"> not connected </h1>
    <script src="socket.io/socket.io.js"></script>
    <script src="sketch.js"></script>
  </body>
</html>

/public/sketch.js

var socket = io();
socket.on('connect', function() {
    document.getElementById("socketio").innerHTML = "socket connected";
});
  1. 将上述服务器代码粘贴到server.js文件
  2. 将上述html代码粘贴到public/index.html文件
  3. 将客户端javascript代码粘贴到public/sketch.js文件
  4. 运行npm install
  5. 使用node server
  6. 启动服务器

    它应该工作,然后你可以在它上面构建更多东西。