Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
6,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. var express = require('express');
  2. var app = express();
  3. var serv = require('http').Server(app);
  4.  
  5. app.get('/',function(req, res) {
  6. res.sendFile(__dirname + '/client/index.html');
  7. });
  8. app.use('/client',express.static(__dirname + '/client'));
  9.  
  10. serv.listen(2000);
  11. console.log("Server started.");
  12.  
  13.  
  14. var io = require('socket.io')(serv,{});
  15. io.sockets.on('connection', function(socket){
  16. console.log('socket connection');
  17.  
  18. socket.on('happy',function(data){
  19. console.log('happy because ' + data.reason);
  20. });
  21.  
  22. socket.emit('serverMsg',{
  23. msg:'hello',
  24. });
  25.  
  26. });
  27.  
  28.  
  29.  
  30. <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
  31. <script>
  32. var socket = io();
  33.  
  34. var random = Math.random();
  35.  
  36. var happy = function(){
  37. socket.emit('happy',{
  38. reason:'its my birthday' + random
  39. });
  40. }
  41.  
  42. socket.on('serverMsg',function(data){
  43. console.log(data.msg);
  44. });
  45.  
  46.  
  47. </script>
  48.  
  49. <button onclick="happy()">Happy</button>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement