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

查看:174
本文介绍了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.

角是否支持呢?或者,我需要使用类似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的,所以你并不需要一个额外的客户端框架来使用它。请看看这个<一个href=\"https://github.com/vtortola/WebSocketListener/blob/master/samples/TerminalServer/TerminalServer.Web/content/scripts/app.js\">$connection服务于这个声明<一个href=\"https://github.com/vtortola/WebSocketListener/wiki/WebSocketListener-Terminal-Server\">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.

请求/响应:当客户端发送一个消息,一个请求ID(或的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天全站免登陆