Socket.io 入门

Socket.io

Vue 中使用

NPM 安装
  npm install vue-socket.io --save
  npm install --save socket.io-client
引用

详情

  // main.js

  import VueSocketIO from 'vue-socket.io'
  Vue.use(new VueSocketIO({
      debug: true,
      // 服务器端地址
      connection: 'http://localhost:3000',
      vuex: {
      }
  }))
发送消息和监听消息

例:

    //发送信息给服务端
    this.$socket.emit('login',{
      username: 'username',
      password: 'password'
    });
    //接收服务端的信息
    this.sockets.subscribe('relogin', (data) => {
      console.log(data)
    })

学习文档

w3cschool资料

官方文档

方法(具体示例)

    io.on('connect', onConnect);

    function onConnect(socket){
      // 发送给当前客户端
      socket.emit(
        'hello', 
        'can you hear me?', 
        1, 
        2, 
        'abc'
      );
      // 发送给所有客户端,除了发送者
      socket.broadcast.emit(
        'broadcast', 
        'hello friends!'
      );
      // 发送给同在 'game' 房间的所有客户端,除了发送者
      socket.to('game').emit(
        'nice game', 
        "let's play a game"
      );

      // 发送给同在 'game1' 或 'game2' 房间的所有客户端,除了发送者
      socket.to('game1').to('game2').emit(
        'nice game', 
        "let's play a game (too)"
      );
      // 发送给同在 'game' 房间的所有客户端,包括发送者
      io.in('game').emit(
        'big-announcement', 
        'the game will start soon'
      );
      // 发送给同在 'myNamespace' 命名空间下的所有客户端,包括发送者
      io.of('myNamespace').emit(
        'bigger-announcement', 
        'the tournament will start soon'
      );
      // 发送给指定 socketid 的客户端(私密消息)
      socket.to(<socketid>).emit(
        'hey', 
        'I just met you'
      );
      // 包含回执的消息
      大专栏  Socket.io 入门 - Renyi的博客n class="nx">socket.emit(
        'question', 
        'do you think so?', 
        function (answer) {}
      );
      // 不压缩,直接发送
      socket.compress(false).emit(
        'uncompressed', 
        "that's rough"
      );
      // 如果客户端还不能接收消息,那么消息可能丢失
      socket.volatile.emit(
        'maybe', 
        'do you really need it?'
      );
      // 发送给当前 node 实例下的所有客户端(在使用多个 node 实例的情况下)
      io.local.emit(
        'hi', 
        'my lovely babies'
      );
    };
服务端
// 监听客户端连接,回调函数会传递本次连接的socket
io.on('connection',function(socket));   

// 给所有客户端广播消息
io.sockets.emit('String',data);   

// 给指定的客户端发送消息
io.sockets.socket(socketid).emit('String', data);   

// 监听客户端发送的信息
socket.on('String',function(data));   

// 给该socket的客户端发送消息
socket.emit('String', data);    
客户端socket.on()监听的事件
  • connect:连接成功
  • connecting:正在连接
  • disconnect:断开连接
  • connect_failed:连接失败
  • error:错误发生,并且无法被其他事件类型所处理
  • message:同服务器端message事件
  • anything:同服务器端anything事件
  • reconnect_failed:重连失败
  • reconnect:成功重连
  • reconnecting:正在重连

例:
socket.on(“String”,function(data)) 监听服务端发送的消息 Sting参数与服务端emit第一个参数相同

// 监听服务信息
socket.on('msg',function(data){
    socket.emit('msg', {rp:"fine,thank you"}); //向服务器发送消息
    console.log(data);
});

// 监听socket断开与重连
socket.on('disconnect', function() {
    console.log("与服务其断开");
});

socket.on('reconnect', function() {
    console.log("重新连接到服务器");
});

原文地址:https://www.cnblogs.com/lijianming180/p/12346055.html