Nodejs socketIO
2907 ワード
//
Document
var socket = io();// socketIO, socket
$(function () {
socket.emit('chats','zzw');// , socket.id
console.log(socket);
$('form').submit(function(e){//form
e.preventDefault(); // prevents page reloading
socket.emit('chat message', $('#m').val());// ,
$('#m').val('');//
return false;
});
socket.on('chat message', function(msg){// ,
$('#messages').append($('<li>').text(msg));
});
});
//
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function (req, res) {//
res.sendFile(__dirname + '/socketDemo.html');//
});
let socketWall;
io.on('connection', function (socket) {// connection
socket.on('chats',function(msg){// , socket.id
socketWall = msg;
console.log(' '+socketWall+' ');
});
socket.on('chat message', function (msg) {// ,
console.log(socketWall);
io.emit('chat message', ' '+socketWall+' :'+msg);// emit
});
socket.on('disconnect', function () {//
console.log(' '+socketWall+' ');
});
});
http.listen(8100, function () {//
console.log('listening on *:8100');
});