##WebSocket looks pretty, but I don't use it now.
To transfer messages between clients and server, WebSocket is more efficient and faster than normal HTTP.
But, WebSocket is unreliable, you don't know anything if the clients missed some messages because the connection was broken in a while. How about if the messages are critical for your clients.
You can't stand with any mistake may be appeared in you application system. So, you use HTTP Pooling strategy.
It's sorrowful that when you know a new and better technology but can't use it with enough confidence :(
##How to make WebSocket reliable?
The naturally way to use WebSocket is as follow
But it's not enough for [reliable] communications.
The [reliable] part means that the message receiver received the message successfully.
How to make sure the receiver have received the message.
It's simple. Just let the receiver tell the sender: "Ok, I have gotten the message." If the sender didn't know the receiver already read the message, send it later again. That's all :)
But, how to prevent the receiver reaction the same message more than once. The answer is [uniqueId]. After the receiver gets a message. Checks the uniqueId, if the message already read. Reply the sender "Ok, I have gotten the message. Don't send it to me again. :)". Then the sender mark the message as [received(true)], and never re-send the message again.
##Essential items to make reliable message communication application:
Sender's message storage list:
// If the message marked as Received(true), there's no need to save it.
Receiver's message storage list:
// All the message received
* The receiver's storage will become huge after a long time. And checking a message existed-status in a long list will be slow. OK, you can do more. Let the sender replay the receiver "I know you've received the message!".
Receiver's message storage thinner list:
In this list, the message sender may send the message again because the sender doesn't make sure the receiver already received it. Keep the list to prevent the receiver reaction more than once.
##A reliable message communication:
Sender(send may more than once) >>> messageWithUniqueId >>> Receiver
Sender <<< I've gotten the message <<< Receiver (received may more than once, but reaction just once)
Sender >>>>>>> I know you received the message >>>>>>> Receiver
##It's about reliable, not about WebSocket.
A: Hey, did you hear me?
B: Yes, I did.
Make sure your listener gets what you say. It's just a communication skill. WebSocket is just a message channel. You can use any other channels if you like, even Express Mail Service :)
That's all. I hope you like it :)
给个赞 1 人点赞
收藏 0 人收藏
- 5Webpack 一个工程输出任意页面
- 6Vue React Zepto Jquery Angular UI 组件搜了一大堆
- 7打开APP或者去下载 Open or download
- 8返回上一页，或者回主页 Go back or go home
- 9localStorage 构建WEB客户端实时数据库
- 10HTTP 如何分分钟升级到 HTTPS ?
- 11➣ 指南针 [ HTML5+手机传感器 · 第二弹 ]
- 14荐文《About face》《 The Guard With Three Questions 》
- 15jquery.Confirm 确认框 第三版【延迟确认 · 防手抖】