当前位置: 首页 > news >正文 上海培训网站建设淘宝的前100个关键词排名 news 2025/11/6 0:37:39 上海培训网站建设,淘宝的前100个关键词排名,网站更换服务器 备案,东莞百度搜索网站排名WebSocket是一种在单个TCP连接上进行全双工通信的协议#xff0c;其设计的目的是在Web浏览器和Web服务器之间进行实时通信#xff08;实时Web#xff09;。 WebSocket协议的优点包括#xff1a; 1. 更高效的网络利用率#xff1a;与HTTP相比#xff0c;WebSocket的握手…WebSocket是一种在单个TCP连接上进行全双工通信的协议其设计的目的是在Web浏览器和Web服务器之间进行实时通信实时Web。 WebSocket协议的优点包括 1. 更高效的网络利用率与HTTP相比WebSocket的握手只需要一次之后客户端和服务器端可以直接交换数据 2. 实时性更高WebSocket的双向通信能够实现实时通信无需等待客户端或服务器端的响应 3. 更少的通信量和延迟WebSocket可以发送二进制数据而HTTP只能发送文本数据并且WebSocket的消息头比HTTP更小。 简单使用示例 1. 客户端JavaScript代码 javascript //创建WebSocket对象 var socket new WebSocket(ws://localhost:8080/);//建立连接后回调函数 socket.onopen function(event) {console.log(WebSocket连接建立成功); };//接收到消息后回调函数 socket.onmessage function(event) {console.log(接收到消息 event.data); };//错误回调函数 socket.onerror function(event) {console.log(WebSocket连接发生错误); };//关闭回调函数 socket.onclose function(event) {console.log(WebSocket连接关闭); };//发送消息 socket.send(hello server); 2. 服务器端Java代码 java import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.HashSet; import java.util.Set;public class WebSocketServer {//存储所有连接到服务器的WebSocket对象private static SetWebSocket webSockets new HashSet();public static void main(String[] args) throws IOException {//创建ServerSocketServerSocket serverSocket new ServerSocket(8080);System.out.println(服务器已启动监听端口8080);//循环等待客户端连接while (true) {//创建Socket对象Socket socket serverSocket.accept();//创建WebSocket对象存储到集合中WebSocket webSocket new WebSocket(socket);webSockets.add(webSocket);//启动线程处理该WebSocket连接new Thread(webSocket).start();System.out.println(客户端已连接 socket.getInetAddress().getHostAddress());}}//广播消息给所有连接到服务器的WebSocket对象public static void broadcast(String message) {for (WebSocket webSocket : webSockets) {try {webSocket.sendMessage(message);} catch (IOException e) {e.printStackTrace();}}} } 3. 服务器端WebSocket代码 java import java.io.IOException; import java.io.InputStream; import java.net.Socket;public class WebSocket implements Runnable {private Socket socket;private InputStream inputStream;public WebSocket(Socket socket) throws IOException {this.socket socket;this.inputStream socket.getInputStream();}//接收消息public String receiveMessage() throws IOException {byte[] buffer new byte[1024];int len inputStream.read(buffer);return new String(buffer, 0, len);}//发送消息public void sendMessage(String message) throws IOException {socket.getOutputStream().write(message.getBytes());}Overridepublic void run() {try {while (true) {String message receiveMessage();System.out.println(接收到消息 message);WebSocketServer.broadcast(message);}} catch (IOException e) {e.printStackTrace();} finally {try {socket.close();} catch (IOException e) {e.printStackTrace();}}} } 文章转载自: http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.gpkjx.cn.gov.cn.gpkjx.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.zcckq.cn.gov.cn.zcckq.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.lxhny.cn.gov.cn.lxhny.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn http://www.morning.gbfck.cn.gov.cn.gbfck.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.mltsc.cn.gov.cn.mltsc.cn http://www.morning.qncqd.cn.gov.cn.qncqd.cn http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.jjzxn.cn.gov.cn.jjzxn.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.kybjr.cn.gov.cn.kybjr.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.nccqs.cn.gov.cn.nccqs.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.msbmp.cn.gov.cn.msbmp.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.wpsfc.cn.gov.cn.wpsfc.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn 查看全文 http://www.tj-hxxt.cn/news/280851.html 相关文章: 专业做网站app的公司哪家好企业网站建设条件 wordpress建站图片效果wordpress 删除版权信息 好用的网站链接网站模板手机 做外贸可以用哪些网站那个网址怎么找 网站制作外包价格网站视频站建设教程和 优秀网站菜单株洲专业网站建设 男科医院网站建设管理类培训课程 云浮网站设计公司网站建设的意义方案 建立一个网站的英文wordpress 修改后台 租服务器的网站手机人才网怎么投简历 莱芜网站建设案例深圳福田区住房和建设局网站官网 河南省交通基本建设质量检测监督站网站杨中市网站建设 网站水晶头怎么做最好的网站建设系统 上海做网站公司做网站的公司做网站引流到天猫 潜山做网站未来中森网站建设 请问做网站和编程哪个容易些货代怎么找客户 现在做个企业网站一般多少钱关于校园推广的软文 网站建设与管理课程视频南宁建设网站公司 python做网页零距离seo typecho前端建站工具百度官网首页登陆 公司网站维护与更新优秀网页设计作品文字分析 网站建设义乌wordpress反应慢 建站行业市场京东联盟建网站 网站模板 登陆推广app怎么做 百度指数 多少流量 网站名河南省建设网站首页 h5网站模板如何让百度收录自己的网站 专业的河南网站建设公司哪家好网站建设xml下载 最经典最常用的网站推广方式网站设计服务有哪些 如何设置网站名字网站建设典型发言 小游戏大全网站湖南郴州建设局网站