import { defineStore } from 'pinia';
import io from 'socket.io-client'
import { getToken } from '/@/utils/auth';

interface SocketState {
  robotInfo?: any;
}

export const useSocketStore = defineStore({
  id: 'app-socket',
  state: (): SocketState => ({
    robotInfo: null,
  }),
  getters: {
    getRobotInfo(): any {
      return this.robotInfo || {};
    },
  },
  actions: {
    setRobotInfo(info: any) {
      this.robotInfo = info;
    },
    startSocket() {
      const socket = new WebSocket(`ws://101.34.171.164:8088/websocket?token=${getToken()}`);
      socket.onopen = () => {
        console.log('连接成功');
        socket.send("Hello server!");
      };
      socket.onmessage = (event) => {
        console.log('Message from server ', event.data);
      };
      socket.onclose = () => {
        console.log("WebSocket is closed now.");
      };

      // const socket = io(`wss://101.34.171.164:8088/websocket?token=${getToken()}`);
      // //监听与服务器端的连接成功事件
      // socket.on('connect',function(){
      //   console.log('连接成功');
      //   socket.send('welcome');
      // });
      // //客户端收到服务器发过来的消息后触发
      // socket.on('message',function(message){
      //   console.log(message);
      // });
      // //监听与服务器端断开连接事件
      // socket.on('disconnect',function(){
      //   console.log('断开连接');
      // });
    },
  },
});