简体   繁体   English

如何使用WebSockets和JSON / RESTful API的AngularJS

[英]How to AngularJS with WebSockets and JSON/RESTful APIs

I have been building Rails for a long time and (recently) AngularJS for the last 3 months. 我已经建立了很长时间的Rails,最近三个月(最近)都使用AngularJS。 It's my first attempt with WebSockets and I am creating a test project with this module https://github.com/AngularClass/angular-websocket . 这是我第一次尝试使用WebSockets,并且正在使用此模块https://github.com/AngularClass/angular-websocket创建一个测试项目。 I want to be able to view Twitter or Weather data for example from public APIs. 我希望能够从公共API中查看Twitter或天气数据。

I have followed the instructions and scaffolded a new project with yeoman but I am missing a part: How do you connect the RESTful GET data with the "ws:" websocket stream ? 我已按照说明进行操作,并用yeoman搭建了一个新项目,但是我缺少一个部分:如何将RESTful GET数据与“ ws:” websocket流连接? Do I need a server of some sort ? 我需要某种服务器吗?

EDIT: I'll rephrase my question: What do I need in order to get http data via websockets ? 编辑:我将重述我的问题:我需要什么才能通过websockets获得http数据? Do I need to build a websockets server in my backend ? 我需要在后端构建一个websockets服务器吗?

If your goal is to have a server poll a Twitter end point and then send a websocket message to your angular app, then yes, you will need a server. 如果您的目标是让服务器轮询Twitter端点,然后将websocket消息发送到您的角度应用程序,那么是的,您将需要一台服务器。

So you'll need to create an app server somewhere using your favorite language. 因此,您需要使用自己喜欢的语言在某个地方创建应用服务器。 It doesn't have to be on the same domain or even have to be the same server that serves out your angular app. 它不必在同一域中,甚至不必在提供您的角度应用程序的服务器上。

Then write some code that hits the Twitter end point you are interested in at whatever interval you desire. 然后编写一些代码,以所需的时间间隔到达您感兴趣的Twitter端点。 Keep in mind though that Twitter could have a limit on the number of requests that you can send during a time period. 请记住,尽管Twitter可能会限制您在一段时间内可以发送的请求数量。

Then you could keep track of all the Twitter data that you received before and whenever you get back more data from Twitter, then you can send out a Websocket message with the new data to all your connected Websocket clients. 然后,您可以跟踪以前收到的所有Twitter数据,以及每当您从Twitter取回更多数据时,就可以向所有连接的Websocket客户端发送Websocket消息和新数据。

Whatever language or framework you decide to use to build your server, more than likely there will be a Websocket library that you could use to make things a little easier. 无论您决定使用哪种语言或框架来构建服务器,都很有可能会使用Websocket库来简化事情。

I'm not saying this is the best way to accomplish your goal, but just judging by your original post, this seems like it might be one way of getting stuff done. 我并不是说这是实现目标的最佳方法,但是仅从您的原始帖子来看,这似乎是完成工作的一种方法。

The same thing could be accomplished without using Websockets at all though. 即使完全不使用Websockets,也可以完成同一件事。 With this scenario, you're having a server do all the polling and merging of data. 在这种情况下,您需要让服务器执行所有数据的轮询和合并。 You could just have your Angular app (or clients) do all the polling and merging as well. 您也可以让Angular应用程序(或客户端)完成所有轮询和合并。

Websocket protocol is totally different with http(or https) Websocket协议与http(或https)完全不同

RESTful Get that you want to connect is possible only on http protocol. 您要连接的RESTful Get仅在http协议上可用。

And Twitter does not provide websocket interface. 而且Twitter不提供websocket界面。

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

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