5 Sending and Receiving Messages




   
            官网英文参考:

            中文解析:
                    Websocket 可以发送和接收文本信息和二进制信息,另外也可以发送ping 结构和接收pong结构信息。这一节描述怎么使用session和remoteendpoint 接口实现发消息给对方(connected peer),以及用onMessage 注解方式从对方那里接收消息。     

                    5.1 Sending Messages

                        下面是endpoint发送消息 必要的几步。
                    1.从connection获取session对象。
                        session对象作为endpoint所含方法中的一个参数是可用,(具体参数和方法上一节表中有列举),当你发送的消息是回复对方的,那么在收到消息的方法中已经保留了可用的session对象(如注入的Onmessage 方法)。如果你发的消息不是回复别人的,那么你需要存取session对象,在调用Onopen方法的时候。
                    2.用session对象获取remoteendpoint 对象。
                        Session.getBasicRemote 方法和Session.getAsyncRemote 方法分别返回的是RemoteEndpoint.Basic和RemoteEndpoint.Async 对象。RemoteEndpoint.Basic接口提供阻塞式的消息发送方式,RemoteEndpoint.Async 提供的非阻塞式的消息传输方式。
                    3.用remoteEndpoint对象发送消息给对方。
                       下面有一系列的方法,你可以用来发送消息给对方。       
  • void RemoteEndpoint.Basic.sendText(String text)

    Send a text message to the peer. This method blocks until the whole message has been transmitted.

  • void RemoteEndpoint.Basic.sendBinary(ByteBuffer data)

    Send a binary message to the peer. This method blocks until the whole message has been transmitted.

  • void RemoteEndpoint.sendPing(ByteBuffer appData)

    Send a ping frame to the peer.

  • void RemoteEndpoint.sendPong(ByteBuffer appData)

    Send a pong frame to the peer.

                    第四节的例子 解释了怎么用上述过程回复每个文本消息。  
                5.1.1  Sending Messages to All Peers Connected to an Endpoint(群发)
                            每个endpoint对象只能有且关联到一个连接上,然后有这样一种需求,那就是一个endpoint实体对象要给所有连接的对象发送消息。包括chat 应用和线上拍卖。为了实现这个功能,Session接口提供了getOpenSession 方法。下面举例说明了怎么给所有连接的用户发送消息。
@ServerEndpoint("/echoall")
public class EchoAllEndpoint {
   @OnMessage
   public void onMessage(Session session, String msg) {
      try {
         for (Session sess : session.getOpenSessions()) {
            if (sess.isOpen())
               sess.getBasicRemote().sendText(msg);
         }
      } catch (IOException e) { ... }
   }
}

                    5.2 Receiving Messages

                            OnMessage 注解指定的方法是处理发过来的message,一个endpoint对象最多可以有三个OnMessage 方法,每一个方法处理一种类型的数据(文本格式,二进制格式,pong 格式),下面例子 列出三个OnMessage 方法接收处理三种类型的数据。
@ServerEndpoint("/receive")
public class ReceiveEndpoint {
   @OnMessage
   public void textMessage(Session session, String msg) {
      System.out.println("Text message: " + msg);
   }
   @OnMessage
   public void binaryMessage(Session session, ByteBuffer msg) {
      System.out.println("Binary message: " + msg.toString());
   }
   @OnMessage
   public void pongMessage(Session session, PongMessage msg) {
      System.out.println("Pong message: " + 
                          msg.getApplicationData().toString());
   }
}




1. Reliable sending algorithm: The reliable sending algorithm ensures that the data transmitted over the network is received by the receiver without any errors or loss. This is achieved by using mechanisms such as sequence numbers, acknowledgment messages, and retransmission of lost packets. The sender sends packets with a unique sequence number, and the receiver sends an acknowledgment message for every received packet. If the sender does not receive an acknowledgment message for a specific packet, it retransmits the packet. 2. Reliable receiving algorithm: The reliable receiving algorithm ensures that the data received by the receiver is correct and in the correct order. This is achieved by using mechanisms such as sequence numbers and acknowledgment messages. The receiver checks the sequence number of each received packet and sends an acknowledgment message for each packet. If a packet is received out of order, the receiver stores it until all the previous packets are received, and then it sends a cumulative acknowledgment message. 3. Flow control algorithm: The flow control algorithm ensures that the sender does not overwhelm the receiver with too much data. This is achieved by using mechanisms such as window size and acknowledgment messages. The receiver sends a window size to the sender, which indicates the maximum number of packets that the sender can send before receiving an acknowledgment message. The sender adjusts its transmission rate based on the window size received from the receiver. 4. Congestion control algorithm: The congestion control algorithm ensures that the network is not overloaded with too much traffic. This is achieved by using mechanisms such as slow start, congestion avoidance, and fast retransmit. In the slow start phase, the sender gradually increases its transmission rate until it reaches the maximum window size. In the congestion avoidance phase, the sender increases its transmission rate more slowly to avoid overloading the network. If the sender receives multiple duplicate acknowledgment messages, it assumes that a packet has been lost, and it performs fast retransmit to retransmit the lost packet immediately.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值