dreamworks 前端vue3+vite项目开发模板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.0 KiB

2 years ago
  1. import { defineStore } from 'pinia'
  2. import Socket from '@/socket'
  3. export const useWebSocketStore = defineStore({
  4. id: 'websocket', // id必填,且需要唯一
  5. // state
  6. state: () => {
  7. return {
  8. // 命令websocket 实例
  9. socketCommandInstance: null,
  10. // 事件上报websocket 实例
  11. socketEventInstance: null,
  12. }
  13. },
  14. // actions
  15. actions: {
  16. initCommandSocket() {
  17. const url = import.meta.env.VITE_BASE_WS1_URL
  18. const init = new Socket(url)
  19. init.connect()
  20. init.ws.onmessage = function (ev) {}
  21. this.socketCommandInstance = init
  22. },
  23. sendCommandMsg(message) {
  24. this.socketCommandInstance?.msg(message)
  25. },
  26. initEventSocket() {
  27. const url = import.meta.env.VITE_BASE_WS2_URL
  28. const init = new Socket(url)
  29. init.connect()
  30. init.ws.onmessage = function (ev) {
  31. // console.log(JSON.parse(ev.data))
  32. }
  33. this.socketEventInstance = init
  34. },
  35. sendEventMsg(message) {
  36. this.socketEventInstance?.msg(message)
  37. },
  38. },
  39. })