永发信息网

textwebsockethandler怎么用

答案:2  悬赏:80  手机版
解决时间 2021-02-19 21:01
  • 提问者网友:棒棒糖
  • 2021-02-19 13:42
textwebsockethandler怎么用
最佳答案
  • 五星知识达人网友:梦中风几里
  • 2021-02-19 13:58
WebSocket protocol HTML5种新协议(protocol)实现浏览器与服务器全双工通信(full-duplex) 现中国站实现即通讯(real-time)所用技术都轮询(polling)轮询特定间间隔(time interval)(每1秒)由浏览器服务器发HTTP request由服务器返新数据给客服端浏览器种传统HTTP request d模式带明显缺点 – 浏览器需要断向服务器发请求(request)HTTP request header非面包含数据能值占用带宽 比较新技术做轮询效Comet – 用AJAX种技术虽达全双工通信依需要发请求(reuqest) WebSocket API浏览器服务器需要要做握手作浏览器服务器间形条快速通道两者间直接数据互相传送WebSocket 协议我实现即使服务带两处: 1. Header 互相沟通Header-概 2 Bytes 2. Server Push 服务器主传送数据给客户端 面实现简单PUSH例: 服务端代码: public class InitServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static List socketList; public void init(ServletConfig config) throws ServletException { InitServlet.socketList = new ArrayList(); super.init(config); System.out.println("Server start============"); } public static List getSocketList() { return InitServlet.socketList; } } public class TestWebSocketServlet extends WebSocketServlet{ private static final Logger log = Logger.getLogger(TestWebSocketServlet.class); private static final long serialVersionUID = 1L; //存储链接容器 private static List connsList = new ArrayList(); @Override protected StreamInbound createWebSocketInbound(String subProtocol,HttpServletRequest request) { // TODO Auto-generated method stub return new WebSocketMessageInbound(); } public class WebSocketMessageInbound extends MessageInbound{ @Override protected void onClose(int status) { // InitServlet.getSocketList().remove(this); super.onClose(status); log.debug("onClose"); InitServlet.getSocketList().remove(this); } @Override protected void onOpen(WsOutbound outbound) { log.debug("onOpen"); super.onOpen(outbound); InitServlet.getSocketList().add(this); } @Override protected void onBinaryMessage(ByteBuffer message) throws IOException { // TODO Auto-generated method stub log.debug("onBinaryMessage"); } @Override protected void onTextMessage(CharBuffer message) throws IOException { // TODO Auto-generated method stub log.debug("onTextMessage="+message); // this.getWsOutbound().writeTextMessage(CharBuffer.wrap("====")); // this.getWsOutbound().writeTextMessage(message); //发送给所链接 for (MessageInbound messageInbound : InitServlet.getSocketList()) { CharBuffer buffer = CharBuffer.wrap(message); WsOutbound outbound = messageInbound.getWsOutbound(); outbound.writeTextMessage(buffer); outbound.flush(); } } } } web.xml配置 initServlet com.demo.websocket.InitServlet 1 websocket com.demo.websocket.TestWebSocketServlet websocket /websocket 前台代码:
Send < /html
全部回答
  • 1楼网友:罪歌
  • 2021-02-19 15:25
支持一下感觉挺不错的
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯