套接字io传输错误

时间:2018-04-08 13:31:31

标签: javascript angular socket.io

我正在尝试在与socket io断开连接时向后端发送用户名,当我从套接字连接取消订阅时,我将用户名作为参数传递,它给我带来了问题传输错误。

这是我的代码

客户端'连接'只是一个变量:

      messages = [];
      connectedUsers = [];
      connection;
      message;
      Username = JSON.parse(localStorage.getItem('user')).username;
      users = [];

ngOnDestroy() {
    this.connection.unsubscribe({username: 'emons'});
}

服务器端这是我处理发射控制台日志功能的地方,但是' console.log(usersConnected [i] +'是拼接的')'不起作用,因为这种情况从未得到满足:

const app = express();

const port = 3000;
const server = http.Server(app);
const io = socketIo(server);

var usersConnected = [];

    io.on('connection', function(socket){
        console.log('a user connected');
        /*socket.on('new-connection', function(user) {
            io.sockets.emit('newUser', {type: 'new-connection', username: user});
        }); */


        socket.on('disconnect', function(user){
            console.log(user + '   jsjasdjbskjcbawsjbsajdbasjabdsj')
            for (var i = 0; i < usersConnected.length; i++) {
                if (user.username == usersConnected[i]) {
                    usersConnected.splice(i, 1);
                    console.log(usersConnected[i] +' was spliced')
                };
            } 
            console.log(usersConnected);
            console.log('user disconnected');
        });


        socket.on('add-message', function(msg){
            console.log('added message');
            io.sockets.emit('message', {type: 'new-message', username: msg.user, text: msg.message});
        });
        //trying to get all the users.
        socket.on('chat-connection', (data) => {
            socket.join(data.username);
            console.log(data.username);
            usersConnected.push(data.username);
            io.emit('new-user', usersConnected);
        })


    });

chat.service.ts:

   sendMessage(message) {
      this.socket.emit('add-message', message);
    }

    sendUser(user) {
      console.log(user);//print correctly
      this.socket.emit('chat-connection', user);
    }

    removeUser(user) {
      this.socket.emit('remove-user', user);
    }

    getMessages() {
      let observable = new Observable(observer => {
        this.socket = socketIo(this.url);
        this.socket.on('message', (data) => {
          observer.next(data);
        });

        return () => {
          this.socket.disconnect();
        }
      });
      return observable;
    }

  getUsers() {
    let userObservable = new Observable(observer => {
      this.socket = socketIo(this.url);
      this.socket.on('new-user', (data) => {
        observer.next(data);
      });
    });
    return userObservable;
  }

1 个答案:

答案 0 :(得分:1)

我认为您的服务器端可以是:

<h1 class="tlt">My Title</h1>

<script>
  $(function() {
    $('.tlt').textillate();
  });
</script>
网络断开的

let allClients = []; socket.on('connection', function(client) { allClients.push(client); client.once('disconnect', function() { console.log('Got disconnect!'); client.disconnect(); var i = allClients.indexOf(client); allClients.splice(i, 1); }); client.on('LEAVE_ROOM', function (data) { console.log('Got disconnect!', data); client.disconnect(); var i = allClients.indexOf(client); allClients.splice(i, 1); }); }); 事件。初始化SocketIO Server时可以设置超时:

  

pingTimeout(Number):没有pong数据包的多少ms考虑连接关闭(60000)

客户端可以是:

disconnect