Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

node.js - Socket.io does not work on the ubuntu 16.04

I have an issue with socket.io. When I run my code on my computer (local) I can use http://IP:120/socket.io/socket.io.js. However, on my server (Digital ocean server ubuntu 16.04) I did not see anything at this url. Where is my error? Can anyone help me? I used the createServer and Server function, but neither of them worked.

var app = require('express')();
//var http=require('http').Server(app);
var http=require('http').createServer(app);
var io=require('socket.io')(http);
app.get("/",function(req,res){

    res.sendfile(__dirname+"/asd.html");

});


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


    console.log("A User Connected");


});

var port=120;
http.listen(port,'ServerIP');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution is related to setting up the ports required for the communication.
By default, all ports except :80 are blocked in Digital Ocean

You need to open your port 120 first:

iptables -I INPUT 1 -i eth0 -p tcp --dport 120 -j ACCEPT

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...