samedi 4 avril 2015

Cannot connect remote to nodejs

I'm just trying a few things with nodejs and socket.io. Basically everything works except when I try to connect to the server 'remotely'. For instance when I go to localhost:3000 the page actually gets served, but when I place the 'clients' index.html on my wamp (localhost/js) directory, and I connect to localhost/js(/index.html) I cannot get the socket.io.js file. This returns a 404.



<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="http://ift.tt/Hw6biv"></script>
<script src="http://ift.tt/1pTZRh4"></script>
<script>
var socket = io('localhost',{'port':3000});
socket.on('connect', function(){
socket.emit('authentication', {client: "foo", password: "bar"});
});


$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>


And the server



var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);

require('socketio-auth')(io, {
authenticate: authenticate,
postAuthenticate: postAuthenticate,
timeout: 1000
});



// this part is not needed when trying to 'connect' via the wamp server
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
// end of that comment..

io.on('connection', function(socket){
console.log('connection');
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});





function authenticate(data, callback)
{
console.log('auth')
var username = data.username;
var password = data.password;

return callback(null, 1);
}


function postAuthenticate(socket, data) {
var username = data.username;

// db.findUser('User', {username:username}, function(err, user) {
// socket.client.user = user;
// }
}


I also tried different ports, and I used all kinds of ways to 'load' the socket.io.js file, but I seem to not get it working..


Aucun commentaire:

Enregistrer un commentaire