javascript - Errors doing socket.io chat example node.js -
javascript - Errors doing socket.io chat example node.js -
just started larn node.js multiplayer capabilities:
using windows 7, npm install node.js , components had few issues making me mad
var io = require('socket.io')(http);
was throwing error:
d:\projects\node\chat\index.js:3 var io = require('socket.io').(http); ^ syntaxerror: unexpected token ( @ module._compile (module.js:439:25) @ object.module._extensions..js (module.js:474:10) @ module.load (module.js:356:32) @ function.module._load (module.js:312:12) @ function.module.runmain (module.js:497:10) @ startup (node.js:119:16) @ node.js:901:3
some time more wiered unable handle or no such method defined, etc.
on client side in browser next script not working
<script> var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); homecoming false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); </script>
was throwing (not error) message each action perform in web:
debug - served static content /socket.io.js
after doing lot of effort come 2 easy fixes shared in answer
i may off base of operations here, i'm still learning node myself, appears me reply more simple accepted (on 2014.7.21) suggests; shouldn't have .
between require('socket.io')
, (http);
suggest kind of anonymous function within object returned require
when constructor needs called.
it should this:
var io = require('socket.io')(http);
i not sure on resolution sec problem, did not encounter it. did find needed encapsulate jquery functions within .ready()
standard practice. result looks this:
var socket = io(); $(function () { socket.on('chat message', function (msg) { $('#messages').append($('<li>').text(msg)); }); $('#chatform').submit(function () { socket.emit('chat message', $('#m').val()); $('#m').val(''); homecoming false; }); });
javascript node.js socket.io chat
Comments
Post a Comment