Node.js Socket.IO

2,797 views

Published on

Node.js Socket.IO

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,797
On SlideShare
0
From Embeds
0
Number of Embeds
113
Actions
Shares
0
Downloads
87
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Node.js Socket.IO

  1. 1. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  2. 2. Polling interval
  3. 3. HTTP/1.1 200 OK Content-Type: text/plain Transfer-Encoding: chunked <script>eval("... ")</script>0 <script>eval("... ")</script>0
  4. 4. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  5. 5. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat GET /mychat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat Sec-WebSocket-Version: 13 Origin: http://example.com Client Server
  6. 6. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  7. 7. var myWebSocket = new WebSocket("ws://www.websockets.org"); myWebSocket.onopen = function(evt) { alert("Connection open ..."); }; myWebSocket.onmessage = function(evt) { alert("Received Message: " + evt.data); }; myWebSocket.onclose = function(evt) { alert("Connection closed."); } myWebSocket.send("Hello WebSockets!"); myWebSocket.close();
  8. 8. Web Socket Echo (http://www.websocket.org/echo.html)
  9. 9. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  10. 10. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var http = require("http"); var connect = require("connect"); var socketio = require("socket.io"); var app = connect(); app.use(connect.static("public")); var server = http.createServer(app); var io = socketio.listen(server); io.on("connection", function (socket) { socket.on("message", function (data) { socket.emit("echo", data); }); }); server.listen(8000);
  11. 11. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com <!DOCTYPE html> <html> <head> <script src="/socket.io/socket.io.js"></script> </head> <body> <body> <script> var socket = io.connect("http://localhost"); socket.emit("message", "Hello!"); socket.on("echo", function(data) { document.write(data); }); </script> </body> </html>
  12. 12. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com  io.emit('some event', { for: 'everyone' });  socket.broadcast.emit('some event');
  13. 13. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // Server Side var nsp = io.of('/my-namespace'); nsp.on('connection', function(socket) { console.log('someone connected'); }); nsp.emit('hi', 'everyone!'); // Client Side var socket = io('/my-namespace');
  14. 14. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com io.on('connection', function (socket) { socket.join('some room'); }); io.to('some room').emit('some event');
  15. 15. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  16. 16. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var mi = angular.module('myApp', ['btford.socket-io']); mi.factory('mySocket', function (socketFactory) { return socketFactory(); }); mi.controller('MyCtrl', function(mySocket) { // ... });
  17. 17. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com angular.module(‘socketioApp') .controller('MainCtrl', function($scope, socket) { } socket.forward(‘my-event', $scope); $scope.$on(‘socket:my-event', function (event, serverData) { $scope.data = serverData; }); socket.emit(‘event-from-client’, someData) Allows you to forward the events received by Socket.IO's socket to AngularJS's event system.

×