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

Node - Express - Socket.io

nctunba
May 09, 2012
410

Node - Express - Socket.io

nctunba

May 09, 2012
Tweet

Transcript

  1. 小範例 var express = require('express'); var app = express.createServer(); app.get('/',

    function(req, res){ res.send('Hello World'); }); app.listen(3000);
  2. app.js var express = require('express'); , routes = require('./routes'); app.configure(function(){

    app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); app.get('/', routes.index);
  3. public/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Chatroom</title> <link href="stylesheets/style.css"

    rel="stylesheet" type="text/css"> <script src="javascripts/jquery-1.7.2.min.js"></script> </head> <body> </body> </html>
  4. 小範例 瀏覽器端 socket.on('hello', function (data) { console.log(data); socket.emit('hey', 'hey server');

    }); 伺服器端 socket.emit('hello', 'hello client'); socket.on('hey', function (data) { console.log(data); });
  5. public/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Chatroom</title> <link href="stylesheets/style.css"

    rel="stylesheet" type="text/css"> <script src="javascripts/jquery-1.7.2.min.js"></script> <script src="/socket.io/socket.io.js"></script> </head> <body> </body> </html>