socket.js 617 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import store from '@/store'
  2. let ws = null
  3. export function wsSocket () {
  4. ws = new WebSocket(`${PLATFROM_CONFIG.socketUrl}`)
  5. ws.onopen = function () {
  6. console.log('ws打开')
  7. }
  8. ws.onmessage = function (res) {
  9. const datas = JSON.parse(res.data)
  10. store.dispatch('app/getWsData', datas)
  11. console.log('ws数据', datas)
  12. }
  13. ws.onerror = function (err) {
  14. console.log('ws错误', err)
  15. }
  16. ws.onclose = function () {
  17. console.log('ws关闭')
  18. }
  19. }
  20. export function wsSendData (msg) {
  21. if (ws) {
  22. ws.send(msg)
  23. }
  24. }
  25. export function wsSocketClose () {
  26. if (ws) {
  27. ws.close()
  28. }
  29. }