node.js - Socket.io Chat Tutorial not functioning properly -
node.js - Socket.io Chat Tutorial not functioning properly -
so decided give node.js shot. decided go little chat app break ice. tutorial next straight socket.io site.
http://socket.io/get-started/chat/
i next tutorial word word , have tried re-create , paste code source , getting no when type in text , send it. supposed coming in command prompt, not.
i had user connected , user disconnected messages appear stumped on 1 have followed tutorial step step.
index.js
var app = require('express')(); var http = require('http').server(app); var io = require('socket.io')(http); app.get('/', function(req, res) { res.sendfile('/chat/index.html'); }); io.on('connection', function(socket){ socket.on('chat message', function(msg){ console.log('message: ' + msg); }); }); http.listen(3000, function() { console.log('listening on *:3000'); });
index.html:
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <title>chat</title> <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(''); homecoming false; }); </script> <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> </body> </html>
i assuming fouled not seeing it, though did seek , re-create , paste it. also, if matters using windows 8.1 , chrome. checked around , find no reply problem well. help great.
edit: problem jquery, reason not responding should. after rewriting script in vanilla works intended.
index.js - javascript
<script> var socket = io(); function sendmessage() { var msg = document.getelementbyid('m'); socket.emit('message', msg.value); msg.value = ''; homecoming false; } </script>
html:
<form action="" onsubmit="javascript:sendmessage();"> <input id="m" autocomplete="off" /><button>send</button> </form>
you can activate logging socket.io in chrome typing in console :
localstorage.debug = "socket.io-client:socket"
and on server side :
debug=* node yourfile.js
also, want script executed when document ready, in order utilize jquery need delay bit :
$( document ).ready(function() { var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); homecoming false; }); });
node.js socket.io
Comments
Post a Comment