日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jee websocket搭建总结

發布時間:2025/7/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jee websocket搭建总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、使用框架spring+springmvc+mybatis+jdk7+tomcat7+maven

2、基本原理:

    a. WebSocket協議是一種雙向通信協議,它建立在TCP之上,同http一樣通過TCP來傳輸數據,但是 它和http最大的不同有兩點:

?????????????????? 1.WebSocket是一種雙向通信協議,在建立連接后,WebSocket服務器和Browser/UA都能主動地向對方發送或接收數據,就像Socket一樣,不同的是WebSocket是一種建立在Web基礎上的一種簡單模擬Socket的協議;

?????????????????? 2.WebSocket需要通過握手連接,類似于TCP它也需要客戶端和服務器端進行握手連接,連接成功后才能相互通信。

    b.struts ? 和 websocket ?是2條處理請求的路,被struts 匹配上了 ,自然就到不了 websocket的處? 理邏輯,(http和ws是兩種不同的請求協議,注意框架的攔截器(過濾器))

3、客戶端:

<html> <head><title>Title</title><style>div {margin-left: auto;margin-right: auto;}</style> </head> <body onLoad="startWebSocket()"> <script type="text/javascript">function clog(title) {console.log(title);}var ws = null;function startWebSocket() {if ('WebSocket' in window) {try {ws = new WebSocket("ws://127.0.0.1:80/websocket/tui");} catch (e) {clog("1");}} else if ('MozWebSocket' in window) {ws = new MozWebSocket("ws://127.0.0.1:80/websocket/tui");} else {clog("websocket not support");}ws.onmessage = function (evt) {say(evt.data);};ws.onclose = function (evt) {clog("close!");};ws.onopen = function (evt) {clog("open");};}function sendMsg() {ws.send(document.getElementById('writeMsg').value);}function say(msg) {var div = document.createElement("div");div.innerHTML = msg;document.body.appendChild(div);}</script> <div onClick="say('d')">WebSocket聊天室</div> <div style="border:1px solid #09F"></div> <input type="text" id="writeMsg"/> <input type="button" value="send" onClick="sendMsg()"/></body> </html>

4、服務端:

@ServerEndpoint(value = "/websocket/tui") public class WebSocketDemo{private static final String GUEST_PREFIX = "Guest";private static final AtomicInteger connectionIds = new AtomicInteger(0);private static final Set<WebSocketDemo> connections = new HashSet<>();private final String nickname;private Session session;public WebSocketDemo() {nickname = GUEST_PREFIX + connectionIds.getAndIncrement();}//建立連接 @OnOpenpublic void start(Session session) {this.session = session;connections.add(this);String message = String.format("* %s %s", nickname, "has joined.");System.out.println(message);}//接受消息 @OnMessagepublic void incoming(String message) {System.out.println(message.toString());//broadcast(filteredMessage); broadcast(message.toString());}//客戶端關閉了連接 @OnClosepublic void end() {connections.remove(this);String message = String.format("* %s %s", nickname, "has disconnected.");System.out.println(message);//broadcast(message); }//WebSocket服務出錯 @OnErrorpublic void onError(Throwable t){//log.error("Chat Error: " + t.toString(), t);System.out.println("server has an error!");}private static void broadcast(String msg) {for (WebSocketDemo client : connections) {try {synchronized (client) {client.session.getBasicRemote().sendText(msg);}} catch (IOException e) {//log.debug("Chat Error: Failed to send message to client", e); connections.remove(client);try {client.session.close();} catch (IOException e1) {// Ignore }String message = String.format("* %s %s",client.nickname, "has been disconnected.");broadcast(message);}}} }

注意事項:

???????? 1、訪問攔截器

???????? 2、注意部署的包沖突,例如:javax.websocket只是編譯需要,而發布不需要,maven依賴注意適用范圍

???????? 附:

<!--websocket編譯需要,發布不需要--> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-coyote --> <dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-coyote</artifactId><version>7.0.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-juli --> <dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-juli</artifactId><version>8.5.3</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api --> <dependency><groupId>javax.websocket</groupId><artifactId>javax.websocket-api</artifactId><version>1.1</version><scope>provided</scope> </dependency>

?

轉載于:https://www.cnblogs.com/yuan951/p/6245845.html

總結

以上是生活随笔為你收集整理的jee websocket搭建总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。