如何从6号角

时间:2018-12-01 08:36:27

标签: node.js angular socket.io

这是我使用节点js和套接字io编写的服务器代码。在这里,我创建一个房间,并向特定房间中的发件人以外的所有用户广播一条消息。

server.js

    var io = require('socket.io');
var server = http.createServer().listen(8081);
var socket = io.listen(server);

var roomno = 1;
socket.on('connection', function(client){ 

    client.on('join', function(data) {
        client.join(data.room);  //join in socket room
        console.log(data.user + ' joined in room '+ data.room);
        client.broadcast.to(data.room).emit('new_user_joined',{user : 
        data.user,message :'jas join this room'});
    });

});

chat.service.ts

export class ChatService {
 constructor(private socket: Socket) { }
  joinRoom(data){
  this.socket.emit('join', data);
 }

 newUserJoined() {
   return this.socket.fromEvent('new_user_joined').pipe(map(data => 
   data));
  }

}

app.component.ts

join(){
this.service.joinRoom({user : this.name,room : 'hall'});

this.service.newUserJoined().subscribe(
    result => {
      this.respObj=result;
    }
   );
  }

在上面的代码中,我向套接字中的特定房间广播了一条消息,并尝试从angular接收此消息。但是我没有从app.component.ts收到此消息

0 个答案:

没有答案