lib_socket.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. window.onload = function() {
  10. // init
  11. const socket = io('/lib', {
  12. // 实际使用中可以在这里传递参数
  13. query: {
  14. room: 'lib',
  15. userId: `client_${Math.random()}`,
  16. },
  17. transports: ['websocket']
  18. });
  19. socket.on('connect', () => {
  20. const id = socket.id;
  21. console.log('#connect,', id, socket);
  22. // 监听自身 id 以实现 p2p 通讯
  23. socket.on(id, msg => {
  24. console.log('#receive,', msg);
  25. });
  26. });
  27. socket.on('refreshed', msg => {
  28. if (msg.success) {
  29. $('tr[lib_id="' + msg.id + '"]').removeClass('table-active');
  30. toastr.success('造价文件"' + msg.filename + '"刷新完成');
  31. } else {
  32. $('tr[lib_id="' + msg.id + '"]').find('td:first').append('<a class="fa fa-refresh text-primary" href="/lib/refresh?id=' + msg.id + '" title="再次刷新"></a>');
  33. toastr.error('造价文件"' + msg.filename + '"刷新失败,请重试');
  34. }
  35. if (msg.count === 0) {
  36. $('#refresh-btn').show();
  37. $('#refresh-hint').hide();
  38. } else if (msg.count > 0) {
  39. $('#refresh-btn').hide();
  40. $('#refresh-hint').show().html('正在刷新全部造价文件,未刷新完成的造价文件,暂时无法查看(待刷新:' + msg.count + ')');
  41. }
  42. });
  43. // 系统事件
  44. socket.on('disconnect', msg => {
  45. console.log('#disconnect', msg);
  46. });
  47. socket.on('disconnecting', () => {
  48. console.log('#disconnecting');
  49. });
  50. socket.on('error', () => {
  51. console.log('#error');
  52. });
  53. window.socket = socket;
  54. };