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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

springboot-websocket-netty

發布時間:2024/4/13 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot-websocket-netty 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹

本項目是對springboot官方提供的websocket進行的netty版本封裝,api與原版的完全一致,讓廣大springboot用戶更方便的使用netty版本的websocket。netty與tomcat的相比,占用內存更小,效率更高,在特殊環境下,netty的效率是tomcat的20倍,想更輕松的使用netty版本的websocket,那么現在就來使用它吧!~~

使用說明

  • 添加maven庫
  • <dependency><groupId>com.simon</groupId><artifactId>spring-boot-starter-websocket-netty</artifactId><version>0.0.1</version></dependency>
  • 代碼示例
  • import com.simon.annotation.*;import com.simon.model.Session;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Component;import java.io.IOException;import java.util.concurrent.CopyOnWriteArraySet;@ServerEndpoint(port = 8081,path = "/websocket/{sid}")@Componentpublic class NettyServer {static Log log= LogFactory.getLog(NettyServer.class);//靜態變量,用來記錄當前在線連接數。應該把它設計成線程安全的。private static int onlineCount = 0;//concurrent包的線程安全Set,用來存放每個客戶端對應的MyWebSocket對象。private static CopyOnWriteArraySet<NettyServer> webSocketSet = new CopyOnWriteArraySet<NettyServer>();//與某個客戶端的連接會話,需要通過它來給客戶端發送數據private Session session;//接收sidprivate String sid="";/*** 連接建立成功調用的方法*/@OnOpenpublic void onOpen(Session session,@PathParam("sid") String sid) {this.session = session;webSocketSet.add(this); //加入set中addOnlineCount(); //在線數加1log.info("有新窗口開始監聽:"+sid+",當前在線人數為" + getOnlineCount());this.sid=sid;try {sendMessage("連接成功");} catch (IOException e) {log.error("websocket IO異常");}}/*** 連接關閉調用的方法*/@OnClosepublic void onClose() {webSocketSet.remove(this); //從set中刪除subOnlineCount(); //在線數減1log.info("有一連接關閉!當前在線人數為" + getOnlineCount());}/*** 收到客戶端消息后調用的方法** @param message 客戶端發送過來的消息*/@OnMessagepublic void onMessage(String message, Session session) throws IOException {log.info("收到來自窗口"+sid+"的信息:"+message);this.sendMessage(message);}/**** @param session* @param error*/@OnErrorpublic void onError(Session session, Throwable error) {log.error("發生錯誤");error.printStackTrace();}/*** 實現服務器主動推送*/public void sendMessage(String message) throws IOException {this.session.sendText(message);}/*** 群發自定義消息* */public static void sendInfo(String message,String sid) throws IOException {log.info("推送消息到窗口"+sid+",推送內容:"+message);for (NettyServer item : webSocketSet) {try {//這里可以設定只推送給這個sid的,為null則全部推送if(sid==null) {item.sendMessage(message);}else if(item.sid.equals(sid)){item.sendMessage(message);}} catch (IOException e) {continue;}}}public static synchronized int getOnlineCount() {return onlineCount;}public static synchronized void addOnlineCount() {NettyServer.onlineCount++;}public static synchronized void subOnlineCount() {NettyServer.onlineCount--;}

    總結

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

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