Advertisement
Guest User

Untitled

a guest
Aug 7th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>
  6. Document
  7. </title>
  8. </meta>
  9. <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
  10. <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  11. <script type="text/javascript">
  12. $(document).ready(function () {
  13. $('#login').click(function () {
  14. socket.emit('login', {username: 'a', password: 'a'});
  15. });
  16.  
  17. $('#mesage').click(function () {
  18. socket.emit('message', 'a');
  19. });
  20. });
  21. </script>
  22. <script>
  23. var socket = new io.connect('http://192.168.1.35:3000', {
  24. port : 3000,
  25. transports: ['websocket']
  26. });
  27.  
  28. socket.on('connect', function () {
  29. console.log('connected!');
  30. });
  31.  
  32. socket.on('login', function (message) {
  33. console.log(message);
  34. });
  35.  
  36. socket.on('message', function (message) {
  37. console.log(message);
  38. });
  39.  
  40. socket.on('hello,', function (message) {
  41. console.log(message);
  42. });
  43.  
  44. socket.on('success', function (data) {
  45. console.log(data);
  46. });
  47.  
  48. </script>
  49. </head>
  50. <body>
  51. <h3 id="login">
  52. login
  53. </h3>
  54.  
  55. <h3 id="mesage">
  56. mesage
  57. </h3>
  58. </body>
  59. </html>
  60.  
  61. var socket = require('socket.io'),
  62. express = require('express'),
  63. app = express(),
  64. server = require('http').createServer(app),
  65. io = socket.listen(server),
  66. port = process.env.PORT || 3000,
  67. redis = require("redis"),
  68. redisClient = redis.createClient();
  69.  
  70. socket.on('connection', function (socket) {
  71. socket.on('login', function (data) {
  72. login(data.username, data.password, function (success, value) {
  73. if (success) {
  74. redisClient.exists(data.username, function (err, doesExist) {
  75. if (err) return;
  76. if (!doesExist) {
  77. redisClient.set(data.username, socket.id);
  78. }
  79. else {
  80. redisClient.del(data.username);
  81. redisClient.set(data.username, socket.id);
  82. }
  83. log.info("SOCKET ID: " + socket.id);
  84. });
  85. socket.emit('login', {result: success, id: socket.id});
  86. } else {
  87. socket.emit('login', {result: false, id: socket.id});
  88. }
  89. });
  90. });
  91. socket.on('message', function (username) {
  92. redisClient.get(username, function (err, socketId) {
  93. if (err)
  94. socket.emit('message', err);
  95. io.to(socketid).emit('message', 'for your eyes only');
  96. });
  97. });
  98.  
  99. });
  100.  
  101. Object { result=true, id="/#A5sR-Xo-Owo97NvRAAAB"}
  102.  
  103. user not connected /#A5sR-Xo-Owo97NvRAAAB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement