简体   繁体   English

尝试在专用服务器上实现Node.js和Socket.io

[英]Trying to Implement Node.js and Socket.io on dedicated server

Recently, I have been experimenting with Node.js and Socket.io. 最近,我一直在尝试使用Node.js和Socket.io。 Successfully, I have been able to implement these but only on localhost. 成功地,我已经能够实现这些功能,但是只能在本地主机上实现。 Now I would like use this functionality on my dedicated server. 现在,我想在专用服务器上使用此功能。 I am hosting my server from my own home and I cannot get node and socket to run outside of localhost. 我在自己的家中托管服务器,因此无法使节点和套接字在本地主机之外运行。 The importance of this is so that I can use two different computers while testing out my site. 这样做的重要性在于,在测试站点时,我可以使用两台不同的计算机。 Here is my code as follows: 这是我的代码,如下所示:

app.js:

   var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(80);

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

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

index.html:

    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });

</script>

These are samples that were taken directly off the socket.io site. 这些样本是直接从socket.io网站上获取的。 Now it works on localhost, but not externally. 现在它可以在localhost上运行,但不能在外部运行。 I cannot use it on another computer. 我不能在另一台计算机上使用它。 I was advised to change 建议我改变

var socket = io.connect('http://localhost:8080');
to
 Uncaught ReferenceError: io is not defined mydomain.com/:3
    GET http://mydomain.com/socket.io/socket.io.js  mydomain.com/:1
, but that makes the browser throw the following errors : ,但这会使浏览器抛出以下错误:

  Uncaught ReferenceError: io is not defined mydomain.com/:3 GET http://mydomain.com/socket.io/socket.io.js mydomain.com/:1 

even on my main server computer. 即使在我的主服务器计算机上。 Im using OSX server with a mac mini btw. 我在Mac mini btw上使用OSX服务器。

Any advice would be appreciated: 任何意见,将不胜感激:

It looks like you're listening on port 80 instead of 8080, and you will need to point your script block to your server, so try 看起来您正在侦听端口80而不是8080,并且需要将脚本块指向服务器,因此请尝试

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

needs to be 需要是

<script src="http://mydomain.com:80/socket.io/socket.io.js"></script>

or better yet 或更好

<script src="/socket.io/socket.io.js"></script>

If that doesn't work, make sure you check for any errors under the network tab of your debugger window (presuming you're using chrome) to make sure you're loading that socket.io.js file correctly. 如果这不起作用,请确保检查调试器窗口的“网络”选项卡下的任何错误(假定您使用的是chrome),以确保正确加载了socket.io.js文件。

You don't need to specify server address if it's the same where the js was loaded: 如果加载js的服务器地址相同,则无需指定服务器地址:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect();
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

but you need to make http://mydomain.com/socket.io/socket.io.js available for you browser. 但是您需要使http://mydomain.com/socket.io/socket.io.js对您的浏览器可用。

Ok. 好。 try to run this code: 尝试运行以下代码:

filename: package.json 档名:package.json

{
"name": "test-socketio"
, "version": "0.0.1"
, "private": true
, "dependencies": {
    "socket.io": "*"
},
"engines": {
    "node": "0.8.x"
    ,"npm" : "*"
}

} }

filename: app.js 文件名:app.js

var   express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);

server.listen(80);

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

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log("Recevied: 'my other event'");
    console.log(data);
  });
});

filename: index.html 文件名:index.html

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
    socket.on('news', function (data) {
        console.log("Received news event");
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
</script>

Then run in the code directory: 然后在代码目录中运行:

npm install

and run: 并运行:

sudo node app.js

Please remember that you need to have the root rights to start listening on port 80. 请记住,您需要具有root权限才能开始侦听端口80。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM