javascript - Socket.io tutorial not printing connection to console -
var app = require('express')(); var http = require('http').server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendfile(__dirname + "/index.html"); }); http.listen(3000, function() { console.log('listening on 3000'); }); io.on('connection', function(socket){ console.log('a user connected'); socket.on('disconnect', function(){ console.log('user disconnected'); }); });
i don't understand why won't print "a user connected" when refresh page , wondering why is. below index.html file. following get-started tutorial (http://socket.io/get-started/chat/)
<!doctype html> <html> <head> <title>socket.io chat</title> <style type="text/css"> * { 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: 0.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"> <form action=""> <input id="m" autocomplete="off" /> <button>send</button> </form> </ul> <script src="socket.io/socket.io.js"> var socket = io(); </script> </body>
the problem lies in part of html file:
<script src="socket.io/socket.io.js"> var socket = io(); </script>
you need load socket.io library first so:
<script src="/socket.io/socket.io.js"></script>
then instantiate socket.io object:
<script> var socket = io(); </script>
Comments
Post a Comment