使用 WebSockets 的 9 个应用场景(9 killer uses for WebSockets)

服务器 William 300浏览 0评论

WebSockets enables instant data exchange and is supported by all modern browsers. Put it to use immediately in your Web apps

To all my readers: Fortunately for me I’m currently in Brazil scaling systems, soaking up 90º F (32.2º C) weather, and having my fill of feijoada and caipirinha. Meanwhile, to keep this blog up to your standards, I’ve enlisted my main man Jonathan Freeman to educate you while I’m away. Without further ado, here’s Jonathan — front-end guru, big data specialist, and jazz musician. Enjoy and see you in a couple weeks! — ACO

Users now demand information as soon as it’s available. If you have to refresh the page to get new information, it’s already too late. Luckily, a protocol supported by all modern browsers allows for direct data exchange: WebSockets.

WebSockets支持及时数据交换,并被所有现代浏览器支持。请直接在你的web apps上使用它吧。

至所有我的读者:幸运的是我目前在巴西进行Scaling systems项目,享受32度的气温,吃着咸八宝饭还有凯匹林纳鸡尾酒。同时根据你们的要求更新着博客,我邀请了我主要的成员侨纳琛弗里曼在我离开的时间指导你们。

No other solution exists that provides true bidirectional communication like WebSockets, yet many Web developers still rely on hacks like AJAX long polling. (For the record, I think long polling is highly creative and functional, but a hack nonetheless.) Lack of enthusiasm for WebSockets may be related to a security vulnerability years ago or the lack of browser support at the time, but both issues have been addressed.

Determining whether to use WebSockets for the job at hand is simple:

  • Does your app involve multiple users communicating with each other?
  • Is your app a window into server-side data that’s constantly changing?

If you answered yes to either of these questions, consider using WebSockets. If you’re still unsure and want some inspiration, here are a few killer use cases.

没有其他技术能够像WebSocket一样提供真正的双向通信,许多web开发者仍然是依赖于ajax的长轮询来实现。(注:我认为长轮询是富于创造性和多功能性的,虽然这只是一个不太完美的解决办法(hack))对Websocket缺少热情,也许是因为多年前他的安全性的脆弱,抑或者是缺少浏览器的支持,不管怎样,这两个问题都已经被解决了。

决定手头的工作是否需要使用WebSocket技术的方法很简单:

  • 你的应用提供多个用户相互交流吗?
  • 你的应用是展示服务器端经常变动的数据吗?

如果你的回答是肯定的,那么请考虑使用WebSocket。如果你仍然不确定,并想要更多的灵感,这有一些杀手锏的案例。

1. Social feeds
One of the benefits of social apps is knowing what all your friends are doing when they do it. Sure, it’s a little creepy, but we all love it. You don’t want to wait minutes to find out a family member won a pie-baking contest or a friend has become engaged. You’re online, so your feed should update in real time.

2. Multiplayer games
The Web is quickly coming into its own as a gaming platform. Without having to rely on plug-ins (I’m looking at you, Flash) Web developers are now able to implement and experiment with high-performance gaming in the browser. Whether you’re dealing with DOM elements, CSS animations, HTML5 canvas or you’re experimenting with WebGL, efficient interaction between players is crucial. I don’t want to discover my opponent has moved after I pull the trigger.

1.社交订阅

对社交类的应用的一个裨益之处就是能够即时的知道你的朋友正在做什么。虽然听起来有点可怕,但是我们都喜欢这样做。你不会想要在数分钟之后才能知道一个家庭成员在馅饼制作大赛获胜或者一个朋友订婚的消息。你是在线的,所以你的订阅的更新应该是实时的。

2.多玩家游戏

网络正在迅速转变为游戏平台。在不使用插件(我指的是Flash)的情况下,网络开发者现在可以在浏览器中实现和体验高性能的游戏。无论你是在处理DOM元素、CSS动画,HTML5的canvas或者尝试使用WebGL,玩家之间的互动效率是至关重要的。我不想在我扣动扳机之后,我的对手却已经移动位置。

3. Collaborative editing/coding
We live in the age of distributed dev teams. Working on a copy of a document used to suffice, but then you had to figure out a way to merge all the edited copies together. Version control systems like Git can help with certain files, but you’ll still have to track people down when Git finds a conflict it can’t deal with. With a collaborative solution like WebSockets, we can work on the same document and skip all the merges. It’s easy to see who is editing what and if you’re working on the same portion of a document as someone else.

4. Clickstream data
Being able to analyze how users interact with your website is critical to improving it. The cost of HTTP has forced us to prioritize and collect only the most important data. Then, six months down the line, we realize we should’ve been collecting a different metric — one that looked unimportant but would now shed light on a critical decision. With the overhead of HTTP requests out of the way, you can be less restrictive about the kind of data you’re sending from the client. Want to track mouse movement in addition to page loads? Just send the data through a WebSocket connection to the back end and persist it in your favorite NoSQL store. (MongoDB is good for logging eventslike this.) Now you can play back customer interactions to see what was really going on.

3.协同编辑/编程

我们生活在分布式开发团队的时代。平时使用一个文档的副本就满足工作需求了,但是你最终需要有一个方式来合并所有的编辑副本。版本控制系统,比如Git能够帮助处理某些文件,但是当Git发现一个它不能解决的冲突时,你仍然需要去跟踪人们的修改历史。通过一个协同解决方案,比如WebSocket,我们能够工作在同一个文档,从而省去所有的合并版本。这样会很容易看出谁在编辑什么或者你在和谁同时在修改文档的同一部分。

4.点击流数据

分析用户与你网站的互动是提升你的网站的关键。HTTP的开销让我们只能优先考虑和收集最重要的数据部分。然后,经过六个月的线下分析,我们意识到我们应该收集一个不同的判断标准——一个看起来不是那么重要但是现在却影响了一个关键的决定。与HTTP请求的开销方式相比,使用Websocket,你可以由客户端发送不受限制的数据。想要在除页面加载之外跟踪鼠标的移动?只需要通过WebSocket连接发送这些数据到服务器,并存储在你喜欢的NoSQL数据库中就可以了(MongoDB是适合记录这样的事件的)。现在你可以通过回放用户在页面的动作来清楚的知道发生了什么。

5. Financial tickers
The finance world moves fast — microsecond fast. Our human brains can’t keep up with processing data at that speed, so we write algorithms to do it for us. Even if you’re not dealing with high-frequency trading, however, stale information can only hurt. When you have a dashboard tracking companies you’re interested in, you want to know what they’re worth now, not 10 seconds ago. Use WebSockets to stream that data and no one needs to wait.

6. Sports updates
While we’re on the topic of silly things people are passionate about, let’s talk sports. I’m not a sports guy, but I know what sports guys want. When the Patriots are playing, my brother-in-law surrenders himself to the pace of the game. It’s a trancelike state prone to violent, visceral reactions. I don’t understand it, but I respect the intensity of the connection, so the last thing I want to do is add latency to his experience. If you’re including sports information in your Web app, WebSockets can keep your users up to speed.

5.股票基金报价

金融界瞬息万变——几乎是每毫秒都在变化。我们人类的大脑不能持续以那样的速度处理那么多的数据,所以我们写了一些算法来帮我们处理这些事情。虽然你不一定是在处理高频的交易,但是,过时的信息也只能导致损失。当你有一个显示盘来跟踪你感兴趣的公司时,你肯定想要随时知道他们的价值,而不是10秒前的数据。使用WebSocket可以流式更新这些数据变化而不需要等待。

6.体育实况更新

现在我们开始讨论一个让人们激情澎湃的愚蠢的东西——体育。我不是运动爱好者,但是我知道运动迷们想要什么。当爱国者在打比赛的时候,我的妹夫将会沉浸于这场比赛中而不能自拔。那是一种疯狂痴迷的状态,完全发自内心的。我虽然不理解这个,但是我敬佩他们与运动之间的这种强烈的联系,所以,最后我能做的就是给他的体验中降低延迟。如果你在你的网站应用中包含了体育新闻,WebSocket能够助力你的用户获得实时的更新。

7. Multimedia chat
While there’s no substitute for holding a meeting in person, videoconferences are about as good as it gets when we can’t get everybody in the same room. The videoconference route is plug-in heavy, though, and full of proprietary "goodness." I’m a fan of the open Web, so using WebSockets with getUserMedia API’s and the HTML5 audio and video elements is an obvious win. WebRTC, the logical predecessor to the combination I just outlined, looks promising, but the lack of current browser support eliminates it as a candidate.

7.多媒体聊天

视频会议并不能代替和真人相见,但当你不能在同一个屋子里见到你谈话的对象时,视频会议是个不错的选择。尽管视频会议私有化做的“不错”,但其使用还是很繁琐。我可是开放式网络的粉丝,所以用WebSockets getUserMedia API和HTML5音视频元素明显是个不错的选择。WebRTC的出现顺理成章的成为我刚才概括的组合体,它看起来很有希望,但其缺乏目前浏览器的支持,所以就取消了它成为候选人的资格。

8. Location-based apps
More and more developers are leveraging the GPS capabilities of mobile devices to make theirWeb apps location-aware. If you’re tracking user locations over time (such as running an app that tracks your progress along a route), you’ll be collecting fine-grained data. If you want to update a Web dashboard in real time (say, a track coach monitoring the progress of runners) HTTP is going to be unnecessarily bulky. Leverage the TCP connection a WebSocket uses and let that data fly.

9. Online education
School keeps getting more expensive, while the Internet keeps getting faster and cheaper. Online education can be a great way to learn, especially if you can interact with teachers and other students. WebSockets is the natural choice, allowing for multimedia chat, text chat, and other perks like collaborative drawing on a digital communal chalkboard…

8.基于位置的应用

越来越多的开发者借用移动设备的GPS功能来实现他们基于位置的网络应用。如果你一直记录用户的位置(比如运行应用来记录运动轨迹),你可以收集到更加细致化的数据。如果你想实时的更新网络数据仪表盘(可以说是一个监视运动员的教练),HTTP协议显得有些笨拙。借用WebSocket TCP链接可以让数据飞起来。

9.在线教育

上学花费越来越贵了,但互联网变得更快和更便宜。在线教育是学习的不错方式,尤其是你可以和老师以及其他同学一起交流。很自然,WebSockets是个不错的选择,可以多媒体聊天、文字聊天以及其它优势如与别人合作一起在公共数字黑板上画画…


via:oschina

转载请注明:AspxHtml学习分享网 » 使用 WebSockets 的 9 个应用场景(9 killer uses for WebSockets)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址