简体   繁体   English

websocket 是聊天应用程序的正确技术吗?

[英]Is websocket a right technology for Chat Application?

I created one small chat App using websocket in that user can join chat-room and can chat with multiple user or in group.我使用 websocket 创建了一个小型聊天应用程序,用户可以加入聊天室并可以与多个用户或群组聊天。

May be at a time maximum 80 to 100 users can send message(this is my requirement)可能一次最多 80 到 100 个用户可以发送消息(这是我的要求)

So my question is that websocket is usefull for me?所以我的问题是 websocket 对我有用吗?

Yes it is. 是的。

In fact, a chat is the single most common example of web socket application. 实际上,聊天是Web套接字应用程序中最常见的一个例子。

http://socket.io/get-started/chat/ http://socket.io/get-started/chat/

I think what you want to ask is: "can a server hold good performance with 100+ websocket connections active"? 我想您想问的是:“在100个以上的Websocket连接处于活动状态时,服务器能否保持良好的性能”?

The answer is also: definitively yes. 答案也是:绝对可以。

The proof are benchmarks. 证明是基准。 A single server can handle easily more than 1000 websocket connections without problem. 一台服务器可以轻松处理1000多个websocket连接,而不会出现问题。

See: 看到:

Websockets can be used to allow each client to get updates from your server without periodically polling the server. Websocket可用于允许每个客户端从服务器获取更新,而无需定期轮询服务器。

If you want to be realy responsive (ie show the text as people are typing) then Websockets are what you need. 如果您想真正响应(即在人们打字时显示文本),那么Websockets是您所需要的。 However if you are happy to wait 1second to show a message after it has been send you could also use a periodic get request. 但是,如果您乐意在发送消息后等待一秒钟以显示消息,则也可以使用定期的get请求。

However consider the browsers that will be using your app. 不过,请考虑将要使用您的应用的浏览器。 see: Can I Use Websockets for a compatibly. 请参阅: 我可以兼容使用Websocket

To add to what Matthaus Woolard said, not all browsers support websocket ie Opera mini, so to make your application work-proof, you need to design with websocket and long polling ,.补充一下 Matthaus Woolard 所说的,并非所有浏览器都支持 websocket,即 Opera mini,因此为了使您的应用程序能够正常工作,您需要使用websocket长轮询进行设计。 To detect websocket availability you can use the $.get or $.ajax method to send a request to the WebSocket endpoint.要检测 websocket 可用性,您可以使用 $.get 或 $.ajax 方法向 WebSocket 端点发送请求。 If the connection is successful, the onopen event will be triggered, and you can use the onmessage event to handle incoming messages.如果连接成功,会触发onopen事件,可以使用onmessage事件来处理传入的消息。 If the connection is unsuccessful, the onerror or onclose event will be triggered.如果连接不成功,将触发 onerror 或 onclose 事件。 Here is an example:这是一个例子:

var socket = new WebSocket("ws://example.com/ws");

socket.onopen = function() {
    console.log("WebSocket connection opened.");
    //run your websocket node.js here
};

socket.onmessage = function(event) {
    console.log("Received message: " + event.data);
    //run your websocket node.js here
};

socket.onerror = function(event) {
    console.log("WebSocket error: " + event);
   //fall back to long polling here
};

socket.onclose = function() {
    console.log("WebSocket connection closed.");
   //fall back to long polling he
};

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

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