用于实时聊天应用的HTML5 Websockets? [英] HTML5 Websockets for Realtime Chat app?

查看:113
本文介绍了用于实时聊天应用的HTML5 Websockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们计划使用Websockets技术制作基于HTML5的聊天应用程序。



所以我的问题是:
$ b $ ol

  • 当前支持 Websockets 的浏览器

  • 如果浏览器不支持它,什么是可能的优雅回退?

    是否有一个polyfill可以提供帮助?


    问候,

    解决方案


    哪些是当前支持Websockets的浏览器?

    正如前面的答案中指出的那样。 / p>

    请参阅:




    如果浏览器不支持它,什么是可能优雅的后备?


    如果您的实时Web服务器仅支持WebSockets ,则下一个最佳选择是使用 web-socket-js 这是一个基于Flash的polyfill,它创建一个 WebSocket 对象,它与本地 WebSocket 对象。



    其他回退 - 需要您的实时Web服务器支持并由JavaScript客户端管理它提供的库 - 是最好的,最差的:
    $ b


    1. EventSource - 一个流媒体连接来自服务器 - >客户端。使用第二个HTTP请求可以实现双向通信。

    2. HTTP Streaming - 使用两个连接来模拟双向连接。消息通过持久的流媒体连接服务器推送 - >客户端

    3. HTTP长轮询 - 也使用两个连接。但是,服务器 - >客户端处于打开状态,直到有新消息可用或发生超时。然后关闭它,如果有数据被设置,它会在响应中传递。

    4. 标准HTTP轮询 - 效率低得多,并且由于潜在的大量浪费了请求。但是,如果您的应用程序提供的更新不会经常发生,并且如果数据在轮询请求之间的时间过时并不重要,那么这可能是一个可接受的解决方案。这里没有'推'。

    选项1.和2.可以通过各种不同的方式实现,具体取决于网页浏览器。这是什么让他们'黑客'。我们现在有用于双向通信的WebSocket和EventSource(服务器发送的事件),它与HTTP Streaming非常相似,并且它支持自动重新连接的额外好处。


    有没有可以提供帮助的polyfill?

    是的,请参阅web-socket-js ,如前所述。

    对于PHP,您最好的选择是棘轮。它不在Apache中运行,所以它不会受到相同的限制 - 它不是建立在请求/响应范例的基础上。



    最常见我现在看到的解决方案是:

    a> - node.js和ruby
  • socket.io - node.js和各种其他端口语言

  • SockJS - erlang,node.js,ruby

  • SignalR - .NET

  • XSockets - .NET



  • 其他选项 - 包括托管服务,如推送者(我在撰写本文时为之工作) - 看看这个实时网络技术指南,我正在维护(并接受捐款)。


    We were planning to make an HTML5 based chat app using the Websockets technology.

    So my question is:

    1. Which are the browsers that support Websockets natively currently as of today?

    2. If a browser does not support it, what is a possible graceful fallback?

    3. Is there a polyfill that can help?

    Regards,

    解决方案

    Which are the browsers that support Websockets natively currently as of today?

    As pointed out in previous answers.

    See:

    If a browser does not support it, what is a possible graceful fallback?

    If your realtime web server only supports WebSockets then the next best option is to use web-socket-js it's a Flash-based polyfill which creates a WebSocket object which an be interacted with in the same ways as the native WebSocket object.

    Additional fallbacks - which need to be supported by your realtime web server and managed by the JavaScript client library it provides - are, ordered best to worst:

    1. EventSource - A single streaming connection from server -> client. Bi-directional communication can be achieved using a second HTTP request.
    2. HTTP Streaming - uses two connections to simulate bi-directional connectivity. Messages are 'pushed' over a persistent 'streaming' connection server -> client
    3. HTTP Long-Polling - also uses two connections. However, the server -> client is opened, held until either a new message is available or a timeout occurs. It's then closed and if any data has been set it is delivered in the response.
    4. Standard HTTP polling - much less efficient and due to the large amount of potentially wasted requests. However, if the updates your app delivers don't happen all that frequently and it doesn't matter if data is 'out of date' for the time between polling requests, then this may be an acceptable solution. There is no 'push' here.

    Options 1. and 2. can be achieved in numerous different ways depending on the web browser. This is what makes them 'hacks'. We now have WebSocket for bi-directional communication and EventSource (server-sent events) which is very similar to HTTP Streaming with the added benefit of it supporting auto-reconnection.

    Is there a polyfill that can help?

    Yes, see web-socket-js as previously mentioned.

    For PHP your best choice is Ratchet. It doesn't run within Apache so it doesn't suffer from the same limitations - it wasn't built with the Request/Response paradigm in mind.

    The most commonly used solutions I see right now are:

    For other options - including hosted services like Pusher (who I work for at the time of writing) - take a look at this realtime web tech guide which I'm maintaining (and accepting contributions towards).

    这篇关于用于实时聊天应用的HTML5 Websockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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