简体   繁体   English

服务器向特定客户端发送事件时,Angular2不要使用socket.io-client(不同的angular2服务)

[英]Do not work socket.io-client with Angular2 when server emit event to specific client (different angular2 service)

I created two service send-message.service.ts 我创建了两个服务send-message.service.ts

import { Injectable } from "@angular/core";
import * as io from 'socket.io-client';

@Injectable()
export class SendMessageService {

  private url = 'http://localhost:4000';
  private socket = io(this.url);

  saveUser(user) {
    this.socket.emit('joining-to-chat', user);
  }

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

}

and receive-message.service.ts receive-message.service.ts

import { Injectable } from "@angular/core";
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import * as io from 'socket.io-client';

@Injectable()
export class ReceiveMessageService {

  private url = 'http://localhost:4000';
  private socket = io(this.url);

  getMessage() {
    let observable = new Observable(observer => {
      this.socket.on('getMessage', (data) => {
        observer.next(data);
      });
      return () => {
        this.socket.disconnect();
      };
    })
    return observable;
  }
}

And my server code is look like following: 我的服务器代码如下所示:

export default (io) => {

  io.on('connect', (socket) => {

    var users = [];

    socket.on('send-message', (data) => {
       io.sockets.in(data.message_to).emit('getMessage', {
        text: data.message,
        from: data.message_from
      })
    })

    socket.on('joining-to-chat', (data) => {
      socket.join(data.username);
      users.push(data.username);
      io.emit('new-user', users)
    })

    socket.on('disconnect', () => {
      console.log('a user disconnected');
    })

  })

}

When send-message and getMessage event in same service server code and client code everything work perfectly as my expectation. 当在相同的服务服务器代码和客户端代码中使用send-messagegetMessage事件时,一切都可以按照我的期望完美地工作。 But if these to event placed in different service I can't emit event two specific client. 但是,如果将这些事件放置在不同的服务中,则无法发出两个特定的客户端事件。 Only io.emit('getMessage', 'msg') and socket.emit('getMessage', 'msg') work correctly. 只有io.emit('getMessage', 'msg')socket.emit('getMessage', 'msg')正常工作。 I am using: 我在用:

Angula2 v4.3.1 Angula2 v4.3.1

"@types/socket.io-client": "1.4.30", 
"socket.io-client": "2.0.3", 
"socket.io": "2.0.3",

I want to know how to use socket.io-client in angular2 and what is the best structure of socket.io-client when my app contain lots of different independent module and services? 我想知道如何在angular2中使用socket.io-client,当我的应用程序包含许多不同的独立模块和服务时,socket.io-client的最佳结构是什么?

The server side code for socket joining and emitting event to specific user is pretty well. 用于套接字加入并向特定用户发出事件的服务器端代码非常好。 But in client side there have two different instance of socket in two different service. 但是在客户端,在两个不同的服务中有两个不同的套接字实例。 First instance of socket which has been created in SendMessageService that joined with socket and another new socket instance in ReceiveMessageService are not same . SendMessageService中创建的与套接字连接的第一个套接字实例与ReceiveMessageService中的另一个新套接字实例不同 So it would not broadcast event as expected. 因此它不会像预期的那样广播事件。 To make it workable two socket in 为了使其可行,两个插座

getMessage() {
    let observable = new Observable(observer => {
      this.socket.on('getMessage', (data) => {
        observer.next(data);
      });
      return () => {
        this.socket.disconnect();
      };
    })
    return observable;
  }

and

saveUser(user) {
   this.socket.emit('joining-to-chat', user);
  }

should be same instance of socket.Though the angular component or module are different those will use getMessage and saveUser service methods should be use a common service or a global service. 应socket.Though的相同实例角分量或模块是不同的那些将使用的getMessagesaveUser服务方法应使用共同的服务或全局服务。

To create a global service follow the link Making global service 要创建全局服务,请单击链接进行全局服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM