Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Socket.io 即時通訊實作

Tommy Chen
December 04, 2014

Socket.io 即時通訊實作

Tommy Chen

December 04, 2014
Tweet

More Decks by Tommy Chen

Other Decks in Programming

Transcript

  1. &YQSFTT var express = require('express'); var app = express(); var

    server = require('http').Server(app); var io = require('socket.io')(server); app.set('views', __dirname + '/views'); app.set('view engine', 'html'); app.engine('html', require('ejs').renderFile); app.get('/', function(req, res, next){ res.render('index'); }); server.listen(4000, function(){ console.log('Server started'); }); 7 ⵹䞕䲿銴 IUUQTTQFBLFSEFDLDPNUPNNZGBTUXFCEFWFMPQNFOUXJUIFYQSFTT
  2. 7JFX <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Chat room</title> </head>

    <body> <script src="/socket.io/socket.io.js"></script> </body> </html> 8 鯺Ⰵ4PDLFUJPⴅ䒭䏨
  3. 姹鵔鎝䜂 9 ⵹畮 var socket = io.connect('http://localhost:4000'); socket.on('message', function(data){ alert(data.content);

    }); 䖕畮 io.on('connection', function(socket){ socket.emit('message', {content: 'Hi there!'}); });  ⢪欽罏鸮箁ⵌ⠿剪㐼  ⠿剪㐼䱺佐ⵌ⢪欽罏涸鸮箁  ぢ⢪欽罏⫄鷑姹鵔鎝䜂  ⢪欽罏䱺佐ⵌ姹鵔鎝䜂