java實(shí)現(xiàn)websocket的兩種方式實(shí)例詳解-創(chuàng)新互聯(lián)

一、介紹

1.兩種方式,一種使用tomcat的websocket實(shí)現(xiàn),一種使用spring的websocket

創(chuàng)新互聯(lián)長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為淮上企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),淮上網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

2.tomcat的方式需要tomcat 7.x,JEE7的支持。

3.spring與websocket整合需要spring 4.x,并且使用了socketjs,對不支持websocket的瀏覽器可以模擬websocket使用

二、方式一:tomcat

使用這種方式無需別的任何配置,只需服務(wù)端一個(gè)處理類,

 服務(wù)器端代碼

package com.Socket; 
import java.io.IOException; 
import java.util.Map; 
import java.util.concurrent.ConcurrentHashMap; 
import javax.websocket.*; 
import javax.websocket.server.PathParam; 
import javax.websocket.server.ServerEndpoint; 
import net.sf.json.JSONObject; 
@ServerEndpoint("/websocket/{username}") 
public class WebSocket { 
 private static int onlineCount = 0; 
 private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>(); 
 private Session session; 
 private String username; 
 @OnOpen 
 public void onOpen(@PathParam("username") String username, Session session) throws IOException { 
  this.username = username; 
  this.session = session; 
  addOnlineCount(); 
  clients.put(username, this); 
  System.out.println("已連接"); 
 } 
 @OnClose 
 public void onClose() throws IOException { 
  clients.remove(username); 
  subOnlineCount(); 
 } 
 @OnMessage 
 public void onMessage(String message) throws IOException { 
  JSONObject jsonTo = JSONObject.fromObject(message); 
  if (!jsonTo.get("To").equals("All")){ 
   sendMessageTo("給一個(gè)人", jsonTo.get("To").toString()); 
  }else{ 
   sendMessageAll("給所有人"); 
  } 
 } 
 @OnError 
 public void onError(Session session, Throwable error) { 
  error.printStackTrace(); 
 } 
 public void sendMessageTo(String message, String To) throws IOException { 
  // session.getBasicRemote().sendText(message); 
  //session.getAsyncRemote().sendText(message); 
  for (WebSocket item : clients.values()) { 
   if (item.username.equals(To) ) 
    item.session.getAsyncRemote().sendText(message); 
  } 
 } 
 public void sendMessageAll(String message) throws IOException { 
  for (WebSocket item : clients.values()) { 
   item.session.getAsyncRemote().sendText(message); 
  } 
 } 
 public static synchronized int getOnlineCount() { 
  return onlineCount; 
 } 
 public static synchronized void addOnlineCount() { 
  WebSocket.onlineCount++; 
 } 
 public static synchronized void subOnlineCount() { 
  WebSocket.onlineCount--; 
 } 
 public static synchronized Map<String, WebSocket> getClients() { 
  return clients; 
 } 
} 

網(wǎng)站題目:java實(shí)現(xiàn)websocket的兩種方式實(shí)例詳解-創(chuàng)新互聯(lián)
本文來源:http://muchs.cn/article24/cedgce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈小程序開發(fā)、微信公眾號、網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)