Reputation: 370
Heello!!!
With the help of this tool, I managed to port forward port 3000.. https://www.yougetsignal.com/tools/open-ports/
The reason I forwarded this port is because I have a chat application I made that works really well on localhost. Sadly, when asking my friends to open up my web chat app (http://mywebsite.com:81/chat/) the javascript stuff doesn't connect to my server over on port 3000...
'here is some of my code:
server.js
const io = require('socket.io')(3000)
const users = {}
io.on('connection', socket => {
// On message receive
socket.on('msg', message => {
//broadcast message
socket.broadcast.emit('msg', {name: users[socket.id], msg: message});
// If message starts with "/" AKA is a command
if(message.startsWith("/")){
message=message.split(" ");
//message[0]= /help
if(message[0] == "/say"){
txt=message.splice(1).join(" "); // GET TEXT AFTER /help
socket.emit('msg', {name: "TomatoBot", msg: txt});
}else if(message[0] == "/help"){
txt=message.splice(1).join(" "); // GET TEXT AFTER /help
socket.emit('msg', {name: "TomatoBot", msg: "help dialog"});
}else if(message[0] == "/list"){
socket.emit('msg', {name: "TomatoBot", msg: users[socket.id]+" connected users:\n"});
}else{
//no commands found, throw error
socket.emit('msg', {name: "TomatoBot", msg: "@"+users[socket.id]+"\nUnknown command: "+message[0]+". Type /help for a list of commands."});
}
}
})
// On user join
socket.on('new', name => {
users[socket.id] = name;
socket.broadcast.emit('new', name);
})
// On user disconnect
socket.on('disconnect', () => {
socket.broadcast.emit('bye', users[socket.id]);
delete users[socket.id];
})
})
script.js (my client side script)
const socket = io(':3000'); // go from localhost:81 to localhost:3000!!
const form = document.getElementById('form');
const input = document.getElementById('input');
const msgbox = document.getElementById('msgbox');
const name = prompt('name?');
socket.emit('new', name);
appendMessage(name+' joined');
// etc etc.....
So as I already said the connection and messages gets thru over on localhost but as soon as I use an external domain/IP nothing works anymore!! When directly accessing mywebsite.org:3000/socket.io/socket.io.js from the external world the connection DOES work!!
Im pretty lost here, it must be due to some stupid error in my code..
Thanks for the help everybody!!
Upvotes: 0
Views: 1727
Reputation: 19
You don't need to use other port than port what you use for http server.
On server side
const http = require(`http`)
const io = require(`socket.io`)(http)
http.createServer(function (req, res) {
// Use this server for http://mywebsite.com:81
}).listen(81)
io.on(`connection`, client => {
// Use this socket for websocket
})
On client side
const socket = io(`http://mywebsite.com:81`)
Upvotes: 1