在我的情况下,在Angular中使用什么最好的方法?

时间:2022-09-11 14:58:55

I need write some methods that works with socket.io in Angular JS.

我需要编写一些与Angular JS中的socket.io一起使用的方法。

  1. SendMessage()
  2. GetMessages()
  3. TypingMessage();

For example, after event ng-click="SendMessage()" I need send message via Socket.io.

例如,在事件ng-click =“SendMessage()”之后,我需要通过Socket.io发送消息。

I have wrote a good tutotial in Stack overflow about service and factory Angular JS. But still can not choose right way what use in my case. Put this methods in Factory method or make service?

关于服务和工厂Angular JS,我在Stack溢出中写了一个很好的tutotial。但仍然无法选择正确的方式在我的情况下使用什么。将此方法放在Factory方法中还是进行服务?

1 个解决方案

#1


1  

I would suggest you have a look at the following git repo:

我建议你看看下面的git repo:

https://github.com/btford/angular-socket-io

I use this in combination with a Python socket server and it works great. You will be able to create your own socket factory from btford's socketFactory which you can use as a normal factory.

我将它与Python套接字服务器结合使用,效果很好。您将能够从btford的socketFactory创建自己的套接字工厂,您可以将其用作普通工厂。

angular.module('myApp', [
  'btford.socket-io'
]).
factory('mySocket', function (socketFactory) {
  var myIoSocket = io.connect('/some/path');

  mySocket = socketFactory({
    ioSocket: myIoSocket
  });

  return mySocket;
});

#1


1  

I would suggest you have a look at the following git repo:

我建议你看看下面的git repo:

https://github.com/btford/angular-socket-io

I use this in combination with a Python socket server and it works great. You will be able to create your own socket factory from btford's socketFactory which you can use as a normal factory.

我将它与Python套接字服务器结合使用,效果很好。您将能够从btford的socketFactory创建自己的套接字工厂,您可以将其用作普通工厂。

angular.module('myApp', [
  'btford.socket-io'
]).
factory('mySocket', function (socketFactory) {
  var myIoSocket = io.connect('/some/path');

  mySocket = socketFactory({
    ioSocket: myIoSocket
  });

  return mySocket;
});