基于JSR-356实现的Tyrus WebSocket框架的消息传递机制初步了解

对阻塞、非阻塞,同步、异步初步了解了,不是太明白,其中大多数将的是对于CPU的使用率及对一个事件的处理效率。 阻塞与非阻塞各有优缺点,在知乎上也看到了下面的结论:

在处理 IO 的时候,阻塞和非阻塞都是同步 IO。 只有使用了特殊的 API 才是异步 IO。


一、Tyrus发送消息的两种方式:


The interface javax.websocket.RemoteEndpoint, part of Java API for WebSocket, is designed to represent the other end of the communication (related to the endpoint), so the developer uses it to send the message. There are two basic interfaces the user may use - javax.websocket.RemoteEndpoint$Basic andjavax.websocket.RemoteEndpoint$Async.


javax.websocket.RemoteEndpoint接口,是JAVA API中的WebSocket的一部分,它被设计为与连接的另一端进行通信,开发人员可以使用它发送消息。有两种基本的接口可以使用:接口javax.websocket.RemoteEndpoint$Basic(同步消息)和javax.websocket.RemoteEndpoint$Async(异步消息)


1:同步消息传递(javax.websocket.RemoteEndpoint$Basic)


is used to send synchronous messages The point of completion of the send is defined when all the supplied data has been written to the underlying connection. The methods for sending messages on the javax.websocket.RemoteEndpoint$Basic block until this point of completion is reached, except forjavax.websocket.RemoteEndpoint$Basic#getSendStream() and javax.websocket.RemoteEndpoint$Basic#getSendWriter() which present traditional blocking I/O streams to write messages. See the example "Sending message via RemoteEndpoint.Basic instance" to see how the whole text message is send. The following example demonstrates a method which sends the partial text method to the peer:


javax.websocket.RemoteEndpoint$Basic接口被用来发送同步消息,当所有提供的数据都被写入到底层连接时,完成发送的标识被确定。通过javax.websocket.RemoteEndpoint$Basic接口发送消息的方法会被阻塞,直到完成发送的标识到达。javax.websocket.RemoteEndpoint$Basic#getSendStream()方法和javax.websocket.RemoteEndpoint$Basic#getSendWriter()方法 通过传统的传统的阻塞I / O流写入消息。


这里的完成发送的标识应该是方法中的isLast参数(Boolean),这里的了解的还不深入,之前一直在找WebSocket客户端处理消息的方式是什么,通过自己的几个小测试应该是多线程的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值