Last active
December 22, 2015 09:29
-
-
Save cayasso/6452202 to your computer and use it in GitHub Desktop.
Socket.IO port issues, when connecting from domain with different port than 80 to a 80 port domain.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!Doctype html> | |
<html lang="en"> | |
<head> | |
</head> | |
<body> | |
<h1>Socket Test</h2> | |
<script src="http://localhost/socket.io/socket.io.js"></script> | |
<script> | |
// This will fail because socket.io includes port 4000 instead of | |
// port 80 | |
var socket = io.connect('http://localhost'); | |
socket.emit('hi', 'hello world'); | |
socket.on('hi', function (data) { | |
console.log(data); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!Doctype html> | |
<html lang="en"> | |
<head> | |
</head> | |
<body> | |
<h1>Socket Test</h2> | |
<script src="http://localhost/socket.io/socket.io.js"></script> | |
<script> | |
// have to add port 80 to the URL otherwise socket.io will | |
// force port 4000 | |
var socket = io.connect('http://localhost:80'); | |
socket.emit('hi', 'hello world'); | |
socket.on('hi', function (data) { | |
console.log(data); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is our regular server with PORT 4000, it only servers | |
// index.html and from this file we will try to connect to | |
// port 80 via socket.io | |
var fs = require('fs'); | |
var http = require('http'); | |
var server = http.createServer(function server(req, res) { | |
res.setHeader('Content-Type', 'text/html'); | |
fs.createReadStream(__dirname + '/index.html').pipe(res); | |
}); | |
// Start server listening | |
server.listen(process.env.PORT || 4000, function(){ | |
console.log('\033[96mlistening on localhost:4000 \033[39m'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "socket-test-port-issue", | |
"version": "0.0.0", | |
"main": "server.js", | |
"dependencies": { | |
"socket.io": "0.9.x" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is our port 80 server, it serves socket.io | |
var io = require('socket.io').listen(80); | |
io.on('connection', function (socket) { | |
console.log('client connected', socket.id); | |
setInterval(function () { | |
socket.emit('hi', 'hello from server'); | |
}, 2000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment