超越 AngularJS 和 WebSockets [英] AngularJS and WebSockets beyond

查看:25
本文介绍了超越 AngularJS 和 WebSockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了这篇帖子,我确实明白其中的区别.但我的脑海里仍然有这个问题.我可以/应该在同一个应用程序/网站中使用它吗?假设我希望 AngularJs 获取内容并更新我的页面,连接到 REST api 和所有最重要的东西.但最重要的是,我还想要实时聊天,或者在收到更新或消息时触发其他客户端上的事件.

I just read this post, and I do understand what the difference is. But still in my head I have the question. Can/Should I use it in the same App/Website? Say I want the AngularJs to fetch content and update my page, connecting to a REST api and all of that top stuff. But on top of that I also want a realtime chat, or to trigger events on other clients when there is an update or a message received.

Angular 支持吗?或者我需要使用 Socket.io 之类的东西来触发这些事件?两者同时使用有意义吗?如果有人可以帮助我或向我指出一些关于这方面的好书,是否有将它们一起使用的目的.

Does Angular support that? Or I need to use something like Socket.io to trigger those events? Does it make sense to use both? If someone could help me or point me to some good reading about that if there is a purpose for using both of them together.

希望我说得够清楚了.感谢您的任何帮助.

Hope I'm clear enough. thank you for any help.

推荐答案

Javascript 支持 WebSocket,因此您不需要额外的客户端框架来使用它.请看看这个 $在这个基于WebSocket的AngularJS应用中声明的连接服务.

Javascript supports WebSocket, so you don't need an additional client side framework to use it. Please take a look at this $connection service declared in this WebSocket based AngularJS application.

基本上你可以监听消息:

Basically you can listen for messages:

   $connection.listen(function (msg) { return msg.type == "CreatedTerminalEvent"; }, 
        function (msg) {
            addTerminal(msg);
            $scope.$$phase || $scope.$apply();
   });

听一次(非常适合请求/响应):

Listen once (great for request/response):

    $connection.listenOnce(function (data) {
        return data.correlationId && data.correlationId == crrId;
    }).then(function (data) {
        $rootScope.addAlert({ msg: "Console " + data.terminalType + " created", type: "success" });
    });

并发送消息:

    $connection.send({
        type: "TerminalInputRequest",
        input: cmd,
        terminalId: $scope.terminalId,
        correlationId: $connection.nextCorrelationId()
    });

通常,由于 WebSocket 连接是双向的,并且有很好的支持,您也可以使用它用于以请求/响应模型从服务器获取数据.您可以拥有两种模型:

Usually, since a WebSocket connection is bidirectional and has a good support, you can also use it for getting data from the server in request/response model. You can have the two models:

  • 发布者/订阅者:客户端声明其对某些主题感兴趣并为该主题的消息设置处理程序,然后服务器在其认为合适时发布(或推送)消息.

  • Publisher/Subscriber: Where the client declares its interest in some topics and set handlers for messages with that topic, and then the server publish (or push) messages whenever it sees fit.

请求/响应:客户端发送带有 requestID(或correlationId)的消息,并监听该 requestId 的单个响应.

Request/response: Where the client sends a message with a requestID (or correlationId), and listen for a single response for that requestId.

不过,如果需要,您可以同时使用 REST 和 WebSocket 来获取数据.

Still, you can have both if you want, and use REST for getting data, and WebSocket for getting updates.

在服务器端,您可能需要使用 Socket.io 或任何服务器端框架才能拥有支持 WebSocket 的后端.

In server side, you may need to use Socket.io or whatever server side framework in order to have a backend with WebSocket support.

这篇关于超越 AngularJS 和 WebSockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆