web-socket.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function showModal(title, content) {
  2. wx.showModal({
  3. title,
  4. content,
  5. showCancel: false
  6. })
  7. }
  8. function showSuccess(title) {
  9. wx.showToast({
  10. title,
  11. icon: 'success',
  12. duration: 1000
  13. })
  14. }
  15. Page({
  16. onShareAppMessage() {
  17. return {
  18. title: 'Web Socket',
  19. path: 'packageAPI/pages/network/web-socket/web-socket'
  20. }
  21. },
  22. data: {
  23. theme: 'light',
  24. socketStatus: 'closed'
  25. },
  26. onLoad() {
  27. this.setData({
  28. theme: wx.getSystemInfoSync().theme || 'light'
  29. })
  30. if (wx.onThemeChange) {
  31. wx.onThemeChange(({theme}) => {
  32. this.setData({theme})
  33. })
  34. }
  35. const self = this
  36. self.setData({
  37. hasLogin: true
  38. })
  39. // qcloud.setLoginUrl(loginUrl)
  40. // qcloud.login({
  41. // success: function(result) {
  42. // console.log('登录成功', result)
  43. // self.setData({
  44. // hasLogin: true
  45. // })
  46. // },
  47. // fail: function(error) {
  48. // console.log('登录失败', error)
  49. // }
  50. // })
  51. },
  52. onUnload() {
  53. this.closeSocket()
  54. },
  55. toggleSocket(e) {
  56. const turnedOn = e.detail.value
  57. if (turnedOn && this.data.socketStatus === 'closed') {
  58. this.openSocket()
  59. } else if (!turnedOn && this.data.socketStatus === 'connected') {
  60. const showSuccess = true
  61. this.closeSocket(showSuccess)
  62. }
  63. },
  64. openSocket() {
  65. // var socket = this.socket = new qcloud.Tunnel(tunnelUrl)
  66. wx.onSocketOpen(() => {
  67. console.log('WebSocket 已连接')
  68. showSuccess('Socket已连接')
  69. this.setData({
  70. socketStatus: 'connected'
  71. })
  72. })
  73. wx.onSocketClose(() => {
  74. console.log('WebSocket 已断开')
  75. this.setData({socketStatus: 'closed'})
  76. })
  77. wx.onSocketError(error => {
  78. showModal('发生错误', JSON.stringify(error))
  79. console.error('socket error:', error)
  80. this.setData({
  81. loading: false
  82. })
  83. })
  84. // 监听服务器推送消息
  85. wx.onSocketMessage(message => {
  86. showSuccess('收到信道消息')
  87. console.log('socket message:', message)
  88. this.setData({
  89. loading: false
  90. })
  91. })
  92. // 打开信道
  93. wx.connectSocket({
  94. url: 'wss://echo.websocket.org',
  95. })
  96. },
  97. closeSocket() {
  98. if (this.data.socketStatus === 'connected') {
  99. wx.closeSocket({
  100. success: () => {
  101. showSuccess('Socket已断开')
  102. this.setData({socketStatus: 'closed'})
  103. }
  104. })
  105. }
  106. },
  107. sendMessage() {
  108. if (this.data.socketStatus === 'connected') {
  109. wx.sendSocketMessage({
  110. data: 'Hello, Miniprogram!'
  111. })
  112. }
  113. },
  114. })