socket-handle.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const socket_io = require('./socket.io-mp')
  2. const {
  3. IM_HOST
  4. } = require('../config/config')
  5. let io = ''
  6. export function initSocket(page, options) {
  7. const defaultOpts = {
  8. customerId: '',
  9. agentId: '',
  10. role: 'agent',
  11. nickName: '',
  12. roomId: '',
  13. isClient: true
  14. }
  15. io = socket_io(IM_HOST, {
  16. path: '/vr-node',
  17. query: Object.assign(defaultOpts, options)
  18. })
  19. const eventName = {
  20. startPlay: 'answer', // 开始语音
  21. someoneInRoom: 'vr_request', // 有人加入房间
  22. someoneLeaveRoom: 'putup', // 有人离开房间
  23. action: 'action', // 点击操作
  24. }
  25. // 事件监听
  26. io.on(eventName.startPlay, function (data) {
  27. console.log(data, 'startplay')
  28. page.startPlay(data)
  29. })
  30. io.on(eventName.someoneInRoom, function (data) {
  31. console.log('有人来了')
  32. console.log(data)
  33. if (data.persons.length < 2) {
  34. return
  35. }
  36. console.log(data)
  37. let user = data.persons.find(item => item.userId === getApp().globalData.userinfo.user_id)
  38. // page.enterAudioRoom(user)
  39. wx.getSetting({
  40. success: (res) => {
  41. if (!res.authSetting['scope.record']) {
  42. wx.authorize({
  43. scope: 'scope.record',
  44. success: (res) => {
  45. // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
  46. console.log('同意', res);
  47. page.enterAudioRoom(user)
  48. },
  49. fail: res => {
  50. console.log('拒绝', res);
  51. cbError && cbError();
  52. }
  53. })
  54. } else {
  55. page.enterAudioRoom(user)
  56. }
  57. }
  58. })
  59. })
  60. io.on(eventName.someoneLeaveRoom, function (data) {
  61. page.someoneLeave(data)
  62. })
  63. io.on(eventName.action, (data) => {
  64. if (data && data.name === 'delegation' && data.role === 'agent') {
  65. page.changeDelegetionStatus()
  66. }
  67. })
  68. return io
  69. }
  70. export function emitSendMsg(target_id) {
  71. let app = getApp()
  72. io.emit('sendMsg', {
  73. user_id: app.globalData.userinfo.user_id,
  74. target_id: target_id
  75. })
  76. }
  77. export function emitInRoom() {
  78. let app = getApp()
  79. io.emit('inRoom', {
  80. user_id: app.globalData.userinfo.user_id,
  81. room_id: app.globalData.room_id
  82. })
  83. }
  84. export function emitLeaveRoom() {
  85. let app = getApp()
  86. io.emit('leaveRoom', {
  87. user_id: app.globalData.userinfo.user_id,
  88. room_id: app.globalData.room_id
  89. })
  90. }
  91. export function disconnect() {
  92. return io.disconnect()
  93. }