Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

362 Zeilen
8.4 KiB

  1. var app = require('express')();
  2. var server = require('http').Server(app);
  3. var io = require('socket.io')(server);
  4. var cookie = require('cookie');
  5. var request = require('superagent');
  6. var { get_conf, get_redis_subscriber } = require('./node_utils');
  7. const log = console.log; // eslint-disable-line
  8. var conf = get_conf();
  9. var subscriber = get_redis_subscriber();
  10. // serve socketio
  11. server.listen(conf.socketio_port, function () {
  12. log('listening on *:', conf.socketio_port); //eslint-disable-line
  13. });
  14. // on socket connection
  15. io.on('connection', function (socket) {
  16. if (get_hostname(socket.request.headers.host) != get_hostname(socket.request.headers.origin)) {
  17. return;
  18. }
  19. if (!socket.request.headers.cookie) {
  20. return;
  21. }
  22. const sid = cookie.parse(socket.request.headers.cookie).sid;
  23. if (!sid) {
  24. return;
  25. }
  26. socket.user = cookie.parse(socket.request.headers.cookie).user_id;
  27. // frappe.chat
  28. socket.on("frappe.chat.room:subscribe", function (rooms) {
  29. if (!Array.isArray(rooms)) {
  30. rooms = [rooms];
  31. }
  32. for (var room of rooms) {
  33. log('frappe.chat: Subscribing ' + socket.user + ' to room ' + room);
  34. room = get_chat_room(socket, room);
  35. log('frappe.chat: Subscribing ' + socket.user + ' to event ' + room);
  36. socket.join(room);
  37. }
  38. });
  39. socket.on("frappe.chat.message:typing", function (data) {
  40. const user = data.user;
  41. const room = get_chat_room(socket, data.room);
  42. log('frappe.chat: Dispatching ' + user + ' typing to room ' + room);
  43. io.to(room).emit('frappe.chat.room:typing', {
  44. room: data.room,
  45. user: user
  46. });
  47. });
  48. // end frappe.chat
  49. let retries = 0;
  50. let join_chat_room = () => {
  51. request.get(get_url(socket, '/api/method/frappe.realtime.get_user_info'))
  52. .type('form')
  53. .query({
  54. sid: sid
  55. })
  56. .then(res => {
  57. const room = get_user_room(socket, res.body.message.user);
  58. socket.join(room);
  59. socket.join(get_site_room(socket));
  60. })
  61. .catch(e => {
  62. if (e.code === 'ECONNREFUSED' && retries < 5) {
  63. // retry after 1s
  64. retries += 1;
  65. return setTimeout(join_chat_room, 1000);
  66. }
  67. log(`Unable to join chat room. ${e}`);
  68. });
  69. };
  70. join_chat_room();
  71. socket.on('task_subscribe', function (task_id) {
  72. var room = get_task_room(socket, task_id);
  73. socket.join(room);
  74. });
  75. socket.on('task_unsubscribe', function (task_id) {
  76. var room = get_task_room(socket, task_id);
  77. socket.leave(room);
  78. });
  79. socket.on('progress_subscribe', function (task_id) {
  80. var room = get_task_room(socket, task_id);
  81. socket.join(room);
  82. send_existing_lines(task_id, socket);
  83. });
  84. socket.on('doc_subscribe', function (doctype, docname) {
  85. can_subscribe_doc({
  86. socket,
  87. sid,
  88. doctype,
  89. docname,
  90. callback: () => {
  91. var room = get_doc_room(socket, doctype, docname);
  92. socket.join(room);
  93. }
  94. });
  95. });
  96. socket.on('doc_unsubscribe', function (doctype, docname) {
  97. var room = get_doc_room(socket, doctype, docname);
  98. socket.leave(room);
  99. });
  100. socket.on('task_unsubscribe', function (task_id) {
  101. var room = 'task:' + task_id;
  102. socket.leave(room);
  103. });
  104. socket.on('doc_open', function (doctype, docname) {
  105. can_subscribe_doc({
  106. socket,
  107. sid,
  108. doctype,
  109. docname,
  110. callback: () => {
  111. var room = get_open_doc_room(socket, doctype, docname);
  112. socket.join(room);
  113. // show who is currently viewing the form
  114. send_users(
  115. {
  116. socket: socket,
  117. doctype: doctype,
  118. docname: docname,
  119. },
  120. 'view'
  121. );
  122. // show who is currently typing on the form
  123. send_users(
  124. {
  125. socket: socket,
  126. doctype: doctype,
  127. docname: docname,
  128. },
  129. 'type'
  130. );
  131. }
  132. });
  133. });
  134. socket.on('doc_close', function (doctype, docname) {
  135. // remove this user from the list of 'who is currently viewing the form'
  136. var room = get_open_doc_room(socket, doctype, docname);
  137. socket.leave(room);
  138. send_users(
  139. {
  140. socket: socket,
  141. doctype: doctype,
  142. docname: docname,
  143. },
  144. 'view'
  145. );
  146. });
  147. socket.on('doc_typing', function (doctype, docname) {
  148. // show users that are currently typing on the form
  149. const room = get_typing_room(socket, doctype, docname);
  150. socket.join(room);
  151. send_users(
  152. {
  153. socket: socket,
  154. doctype: doctype,
  155. docname: docname,
  156. },
  157. 'type'
  158. );
  159. });
  160. socket.on('doc_typing_stopped', function (doctype, docname) {
  161. // remove this user from the list of users currently typing on the form'
  162. const room = get_typing_room(socket, doctype, docname);
  163. socket.leave(room);
  164. send_users(
  165. {
  166. socket: socket,
  167. doctype: doctype,
  168. docname: docname,
  169. },
  170. 'type'
  171. );
  172. });
  173. socket.on('open_in_editor', (data) => {
  174. let s = get_redis_subscriber('redis_socketio');
  175. s.publish('open_in_editor', JSON.stringify(data));
  176. });
  177. });
  178. subscriber.on("message", function (_channel, message) {
  179. message = JSON.parse(message);
  180. if (message.room) {
  181. io.to(message.room).emit(message.event, message.message);
  182. } else {
  183. io.emit(message.event, message.message);
  184. }
  185. });
  186. subscriber.subscribe("events");
  187. function send_existing_lines(task_id, socket) {
  188. var room = get_task_room(socket, task_id);
  189. subscriber.hgetall('task_log:' + task_id, function (_err, lines) {
  190. io.to(room).emit('task_progress', {
  191. "task_id": task_id,
  192. "message": {
  193. "lines": lines
  194. }
  195. });
  196. });
  197. }
  198. function get_doc_room(socket, doctype, docname) {
  199. return get_site_name(socket) + ':doc:' + doctype + '/' + docname;
  200. }
  201. function get_open_doc_room(socket, doctype, docname) {
  202. return get_site_name(socket) + ':open_doc:' + doctype + '/' + docname;
  203. }
  204. function get_typing_room(socket, doctype, docname) {
  205. return get_site_name(socket) + ':typing:' + doctype + '/' + docname;
  206. }
  207. function get_user_room(socket, user) {
  208. return get_site_name(socket) + ':user:' + user;
  209. }
  210. function get_site_room(socket) {
  211. return get_site_name(socket) + ':all';
  212. }
  213. function get_task_room(socket, task_id) {
  214. return get_site_name(socket) + ':task_progress:' + task_id;
  215. }
  216. // frappe.chat
  217. // If you're thinking on multi-site or anything, please
  218. // update frappe.async as well.
  219. function get_chat_room(socket, room) {
  220. var room = get_site_name(socket) + ":room:" + room;
  221. return room
  222. }
  223. function get_site_name(socket) {
  224. var hostname_from_host = get_hostname(socket.request.headers.host);
  225. if (socket.request.headers['x-frappe-site-name']) {
  226. return get_hostname(socket.request.headers['x-frappe-site-name']);
  227. } else if (['localhost', '127.0.0.1'].indexOf(hostname_from_host) !== -1 &&
  228. conf.default_site) {
  229. // from currentsite.txt since host is localhost
  230. return conf.default_site;
  231. } else if (socket.request.headers.origin) {
  232. return get_hostname(socket.request.headers.origin);
  233. } else {
  234. return get_hostname(socket.request.headers.host);
  235. }
  236. }
  237. function get_hostname(url) {
  238. if (!url) return undefined;
  239. if (url.indexOf("://") > -1) {
  240. url = url.split('/')[2];
  241. }
  242. return (url.match(/:/g)) ? url.slice(0, url.indexOf(":")) : url
  243. }
  244. function get_url(socket, path) {
  245. if (!path) {
  246. path = '';
  247. }
  248. return socket.request.headers.origin + path;
  249. }
  250. function can_subscribe_doc(args) {
  251. if (!args) return;
  252. if (!args.doctype || !args.docname) return;
  253. request.get(get_url(args.socket, '/api/method/frappe.realtime.can_subscribe_doc'))
  254. .type('form')
  255. .query({
  256. sid: args.sid,
  257. doctype: args.doctype,
  258. docname: args.docname
  259. })
  260. .end(function (err, res) {
  261. if (!res) {
  262. log("No response for doc_subscribe");
  263. } else if (res.status == 403) {
  264. return;
  265. } else if (err) {
  266. log(err);
  267. } else if (res.status == 200) {
  268. args.callback(err, res);
  269. } else {
  270. log("Something went wrong", err, res);
  271. }
  272. });
  273. }
  274. function send_users(args, action) {
  275. if (!(args && args.doctype && args.docname)) {
  276. return;
  277. }
  278. const open_doc_room = get_open_doc_room(args.socket, args.doctype, args.docname);
  279. const room = action == 'view' ? open_doc_room: get_typing_room(args.socket, args.doctype, args.docname);
  280. const socketio_room = io.sockets.adapter.rooms[room] || {};
  281. // for compatibility with both v1.3.7 and 1.4.4
  282. const clients_dict = ('sockets' in socketio_room) ? socketio_room.sockets : socketio_room;
  283. // socket ids connected to this room
  284. const clients = Object.keys(clients_dict || {});
  285. let users = [];
  286. for (let i in io.sockets.sockets) {
  287. const s = io.sockets.sockets[i];
  288. if (clients.indexOf(s.id) !== -1) {
  289. // this socket is connected to the room
  290. users.push(s.user);
  291. }
  292. }
  293. const emit_event = action == 'view' ? 'doc_viewers' : 'doc_typers';
  294. // notify
  295. io.to(open_doc_room).emit(emit_event, {
  296. doctype: args.doctype,
  297. docname: args.docname,
  298. users: Array.from(new Set(users))
  299. });
  300. }