node.js - Socket.io failing to log chat message in console -


i'm walking through socket.io tutorial on creating chat room, , i'm having trouble getting app log chat message console.

the app logging when user connects & disconnects, not logging when message passed through.

any thoughts?

the relevant parts of index.html page:

<script src="/socket.io/socket.io.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script>  <script>   var socket = io();   $('form').submit(function() {     socket.emit('chat message', $('#m').val());     $('#m').val('');     return false;   }); </script>  <body>   <ul id="messages"><ul>    <form action="">     <input id="m" autocomplete="off" /><button>send</button>   </form> </body> 

my index.js file:

var express = require('express'); var app = express(); var http = require('http').server(app); var io = require('socket.io')(http);  app.use(express.static('public'));  app.get('/', function(req, res){   res.sendfile(__dirname + '/index.html'); });   io.on('connection', function(socket){   console.log('a user connected');    socket.on('disconnect', function(){     console.log('user disconnected');   });    socket.on('chat message', function(msg) {     console.log("message: " + msg);   }); }); 

i've gotten work!

steps solution: - separate script tag event listener separate js file - include js file in same place script tag - load chatlistener when document ready

my files , works beautifully:

index.html:

<script src="/socket.io/socket.io.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script>  <script src="main.js"></script>  <body>   <ul id="messages"><ul>    <form action="">     <input class="chat" id="m" autocomplete="off" />     <input class="btn" type="submit" value="send" />   </form> </body> 

index.js: nothing changed

main.js:

var socket = io();  var chatlistener = function() {   $('form').submit(function() {     socket.emit('chat message', $('#m').val());     $('#m').val('');     return false;   }); }  $(document).ready(function() {   chatlistener(); }) 

in end, think beginner's js mistake; loading script @ top of page (as tutorial suggested), , event listener on form never registering.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -