i'm newbie in nodeJS, after successfully installing socket.io by npm command i'm create simple js as this code:
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs');
function handler( request, response ) {
fs.readFileSync( __dirname + '/index.html', function( error, data ) {
if( error ) throw error;
response.writeHead(200);
response.end( data );
});
};
app.listen( 1377 )
io.sockets.on( 'connection', function( socket ) {
socket.on( 'loginRequest', function( data ) {
login( data );
});
});
function login( data, socket) {
return socket.emit('loginAnswer', true)
};
this file can be run correctly by node application.js
without any problem, now i'm create simple html code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ift.tt/1Abvuox"></script>
<script>
var socket = io('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
<form onsubmit='requestLogin()'>
<input id='username' type='text'>
<input id='password' type='password'>
<input type='submit'>
</form>
</body>
</html>
name of this file is index.html, i'm using xampp and both of files are in htdocs root, htdocs is only have this files, now i'm open localhost:1337
in browser and i get error
after change address to localhost:1337
i get this error:
Unable to connect
Firefox can't establish a connection to the server at localhost:1337.
Aucun commentaire:
Enregistrer un commentaire