Differences between socket.io and web sockets
// ============== /node/app.js ============
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.on('connection', function(client){
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
client.emit('messages', 'Hello from server');
});
client.on('messages', function(data) {
console.log("here message");
console.log("data: "+data);
client.emit('broad', data);
client.broadcast.emit('broad',data);
});
});
server.listen(3001);
// ==========================================
// ============== client side.html ============
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<form id="form" id="chat_form">
<input id="chat_input" type="text">
<input type="submit" value="Send">
</form>
<script>
var socket = io.connect('http://localhost:3001');
socket.on('connect', function(data) {
socket.emit('join', 'Hello World from client');
});
socket.on('messages', function(data) {
alert(data);
});
socket.on('broad', function(data) {
console.log("broad: "+data);
});
$('form').submit(function(e){
console.log("form submit22222");
e.preventDefault();
var message = $('#chat_input').val();
socket.emit('messages', message);
console.log("form submit33333 :"+message);
});
</script>
// ==========================================