如何设置服务器运行Node.js?

时间:2017-02-25 08:29:24

标签: node.js linux socket.io centos7 vps

我有一个VPS为我的游戏运行Server。我使用node.jssocket.io。我找到了一些教程配置我的my-app-name.service文件,如下所示:

[Service]
ExecStart=/usr/bin/node /usr/games/nodeapp/app.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=my-app-name
User=hnp
Group=hnp
Environment=NODE_ENV=production PORT=5000

[Install]
WantedBy=multi-user.target

但是,当我通过socket.io从 mydomain.com 连接到服务器时:

io('http://<my_server_ip>:5000')

我收到了错误:

GET http://<my_server_ip>/socket.io/?EIO=3&transport=polling&t=LfqMeQU net::ERR_CONNECTION_TIMED_OUT

我的app.js

var http = require('http');
var socketIO = require('socket.io');
var port = process.env.PORT || 5000;
var ip = process.env.IP || 'localhost';
server = http.createServer().listen(port, ip, function() {
   console.log('Server running at %s:%s!', ip, port);
});
var io = socketIO.listen(server);
io.set('match origin protocol', true);
io.set('origins', '*:*');

1 个答案:

答案 0 :(得分:0)

这就是我一直在做的事情。我使用Express.js,但是:

var express = require("express");
var app = express();
app.use(express.static(__dirname + "/"));
var http = require("http");
var server = http.createServer(app);

io = require("socket.io")(server);
server.listen(process.env.PORT || 80, "YOUR_IP");

客户端:

var socket = io();