usestudio.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var http = require("http");
  2. var redis = require('redis');
  3. var RDS_PORT = 6379
  4. var RDS_HOST = '127.0.0.1'
  5. var RDS_OPTS = {}
  6. var client = redis.createClient(RDS_PORT, RDS_HOST, RDS_OPTS)
  7. var user = [] // 用户数组
  8. var userRes = { // 用户response对象
  9. }
  10. client.on('ready', () => {
  11. console.log('ready')
  12. })
  13. var server = http.createServer(function (req, res) {
  14. // 发送消息处理函数
  15. if (req.url === '/send' && req.method === "POST") {
  16. // 接收post数据
  17. var post = '';
  18. req.on('data', function (chunk) {
  19. post += chunk;
  20. });
  21. req.on('end', function () {
  22. post = JSON.parse(post);
  23. client.get(post.rece, function (err, val) {
  24. if (err) {
  25. console.log(err)
  26. } else {
  27. var result = userRes[post.rece].end(JSON.stringify(post))
  28. // 第一条消息 初始化
  29. if (val === null) {
  30. var userObj = {}
  31. userObj['msg'] = []
  32. userObj['unread'] = []
  33. if (result) {
  34. // 已读消息
  35. userObj['msg'].push(post)
  36. } else {
  37. // 若为false 证明连接失效 加入离线消息
  38. userObj['unread'].push(post)
  39. }
  40. } else {
  41. // 已经初始化过情况
  42. var userObj = JSON.parse(val)
  43. if (result) {
  44. // 已读消息
  45. userObj['msg'].push(post)
  46. } else {
  47. // 若为false 证明连接失效 加入离线消息
  48. userObj['unread'].push(post)
  49. }
  50. }
  51. client.set(post.rece, JSON.stringify(userObj), redis.print)
  52. }
  53. })
  54. });
  55. // 获取未读消息
  56. } else if (req.url === "/getUnread" && req.method === "POST") {
  57. res.writeHead(200, { "Content-Type": "text/html;application/json;charset=utf-8", 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With' });
  58. // 接收post数据
  59. var post = '';
  60. req.on('data', function (chunk) {
  61. post += chunk;
  62. });
  63. req.on('end', function () {
  64. // 若有离线消息 直接发送
  65. client.get(post, function (err, val) {
  66. if (err) {
  67. console.log(err)
  68. } else {
  69. // 若redis存有该用户数据
  70. if (val !== null) {
  71. // 获取用户数据
  72. var userObj = JSON.parse(val)
  73. // 若有未读消息
  74. if (userObj.unread.length !== 0) {
  75. // 返回未读消息于前台
  76. res.end(JSON.stringify(userObj.unread))
  77. // 将未读消息添加到已读消息
  78. userObj.msg = userObj.msg.concat(userObj.unread)
  79. // 清空未读消息
  80. userObj.unread = []
  81. // 设置用户数据
  82. client.set(post, JSON.stringify(userObj), redis.print)
  83. } else {
  84. res.end(0)
  85. return
  86. }
  87. } else {
  88. res.end(0)
  89. }
  90. }
  91. })
  92. });
  93. // 轮询
  94. } else if (req.url === "/polling" && req.method === "POST") {
  95. res.writeHead(200, { "Content-Type": "text/html;application/json;charset=utf-8", 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With' });
  96. // 接收post数据
  97. var post = '';
  98. req.on('data', function (chunk) {
  99. post += chunk;
  100. });
  101. req.on('end', function () {
  102. // 将response对象加入userRes对象中
  103. userRes[post] = res
  104. if (user.indexOf(post) === -1) {
  105. // 将用户加入user数组中
  106. user.push(post)
  107. }
  108. });
  109. // 获取当前在线用户
  110. } else if (req.url === "/getUser" && req.method === "GET") {
  111. res.writeHead(200, { "Content-Type": "text/html;application/json;charset=utf-8", 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With' });
  112. for (var i in userRes) {
  113. // 向所有用户发送用户数组
  114. userRes[i].end(JSON.stringify(user))
  115. }
  116. }
  117. });
  118. server.listen(1347, "localhost", function () {
  119. console.log("开始监听" + server.address().port + "......");
  120. });