国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解java WebSocket的實現以及Spring WebSocket

詳解java WebSocket的實現以及Spring WebSocket

2020-07-30 16:04akanairen Java教程

這篇文章主要介紹了詳解java WebSocket的實現以及Spring WebSocket ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

開始學習WebSocket,準備用它來實現一個在頁面實時輸出log4j的日志以及控制臺的日志。

首先知道一些基礎信息:

1.java7 開始支持WebSocket,并且只是做了定義,并未實現

2.tomcat7及以上,jetty 9.1及以上實現了WebSocket,其他容器沒有研究

3.spring 4.0及以上增加了WebSocket的支持

4.spring 支持STOMP協議的WebSocket通信

5.WebSocket 作為java的一個擴展,它屬于javax包目錄下,通常需要手工引入該jar,以tomcat為例,可以在 tomcat/lib 目錄下找到 websocket-api.jar

開始實現

先寫一個普通的WebSocket客戶端,直接引入tomcat目錄下的jar,主要的jar有:websocket-api.jar、tomcat7-websocket.jar

?
1
2
3
4
5
6
7
8
9
10
11
public static void f1() {
    try {
      WebSocketContainer container = ContainerProvider.getWebSocketContainer(); // 獲取WebSocket連接器,其中具體實現可以參照websocket-api.jar的源碼,Class.forName("org.apache.tomcat.websocket.WsWebSocketContainer");
      String uri = "ws://localhost:8081/log/log";
      Session session = container.connectToServer(Client.class, new URI(uri)); // 連接會話
      session.getBasicRemote().sendText("123132132131"); // 發送文本消息
      session.getBasicRemote().sendText("4564546");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

其中的URL格式必須是ws開頭,后面接注冊的WebSocket地址

Client.java 是用于收發消息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@ClientEndpoint
public class Client {
 
  @OnOpen
  public void onOpen(Session session) {
    System.out.println("Connected to endpoint: " + session.getBasicRemote());
  }
 
  @OnMessage
  public void onMessage(String message) {
    System.out.println(message);
  }
 
  @OnError
  public void onError(Throwable t) {
    t.printStackTrace();
  }
}

到這一步,客戶端的收發消息已經完成,現在開始編寫服務端代碼,用Spring 4.0,其中pom.xml太長就不貼出來了,會用到jackson,spring-websocket,spring-message

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
 
import com.gionee.log.client.LogWebSocketHandler;
 
/**
 * 注冊普通WebScoket
 * @author PengBin
 * @date 2016年6月21日 下午5:29:00
 */
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
 
  @Autowired
  @Lazy
  private SimpMessagingTemplate template;
 
  /** {@inheritDoc} */
  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(logWebSocketHandler(), "/log"); // 此處與客戶端的 URL 相對應
  }
 
  @Bean
  public WebSocketHandler logWebSocketHandler() {
    return new LogWebSocketHandler(template);
  }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
 
/**
 *
 * @author PengBin
 * @date 2016年6月24日 下午6:04:39
 */
public class LogWebSocketHandler extends TextWebSocketHandler {
 
  private SimpMessagingTemplate template;
 
  public LogWebSocketHandler(SimpMessagingTemplate template) {
    this.template = template;
    System.out.println("初始化 handler");
  }
 
  @Override
  protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    String text = message.getPayload(); // 獲取提交過來的消息
    System.out.println("handMessage:" + text);
    // template.convertAndSend("/topic/getLog", text); // 這里用于廣播
    session.sendMessage(message);
  }
}

這樣,一個普通的WebSocket就完成了,自己還可以集成安全控制等等

Spring還支持一種注解的方式,可以實現訂閱和廣播,采用STOMP格式協議,類似MQ,其實應該就是用的MQ的消息格式,下面是實現

同樣客戶端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args) {
    try {
      WebSocketContainer container = ContainerProvider.getWebSocketContainer();
      String uri = "ws://localhost:8081/log/hello/hello/websocket";
      Session session = container.connectToServer(Client.class, new URI(uri));
      char lf = 10; // 這個是換行
      char nl = 0; // 這個是消息結尾的標記,一定要
      StringBuilder sb = new StringBuilder();
      sb.append("SEND").append(lf); // 請求的命令策略
      sb.append("destination:/app/hello").append(lf); // 請求的資源
      sb.append("content-length:14").append(lf).append(lf); // 消息體的長度
      sb.append("{\"name\":\"123\"}").append(nl); // 消息體
 
      session.getBasicRemote().sendText(sb.toString()); // 發送消息
      Thread.sleep(50000); // 等待一小會
      session.close(); // 關閉連接
 
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

這里一定要注意,換行符和結束符號,這個是STOMP協議規定的符號,錯了就不能解析到

服務端配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * 啟用STOMP協議WebSocket配置
 * @author PengBin
 * @date 2016年6月24日 下午5:59:42
 */
@Configuration
@EnableWebMvc
@EnableWebSocketMessageBroker
public class WebSocketBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {
 
  /** {@inheritDoc} */
  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    System.out.println("注冊");
    registry.addEndpoint("/hello").withSockJS(); // 注冊端點,和普通服務端的/log一樣的
    // withSockJS()表示支持socktJS訪問,在瀏覽器中使用
  }
 
  /** {@inheritDoc} */
  @Override
  public void configureMessageBroker(MessageBrokerRegistry config) {
    System.out.println("啟動");
    config.enableSimpleBroker("/topic"); //
    config.setApplicationDestinationPrefixes("/app"); // 格式前綴
  }
 
}

Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Controller
public class LogController {
 
  private SimpMessagingTemplate template;
 
  @Autowired
  public LogController(SimpMessagingTemplate template) {
    System.out.println("init");
    this.template = template;
  }
 
  @MessageMapping("/hello")
  @SendTo("/topic/greetings") // 訂閱
  public Greeting greeting(HelloMessage message) throws Exception {
    System.out.println(message.getName());
    Thread.sleep(3000); // simulated delay
    return new Greeting("Hello, " + message.getName() + "!");
  }
 
}

到這里就已經全部完成。

?
1
2
template.convertAndSend("/topic/greetings", "通知");
// 這個的意思就是向訂閱了/topic/greetings進行廣播

對于用socktJS連接的時候會有一個訪問 /info 地址的請求

如果在瀏覽器連接收發送消息,則用sockt.js和stomp.js

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function connect() {
   var socket = new SockJS('/log/hello/hello');
   stompClient = Stomp.over(socket);
   stompClient.connect({}, function(frame) {
     setConnected(true);
     console.log('Connected: ' + frame);
     stompClient.subscribe('/topic/greetings', function(greeting) {
       showGreeting(JSON.parse(greeting.body).content);
     });
   });
 }
 
 function disconnect() {
   if (stompClient != null) {
     stompClient.disconnect();
   }
   setConnected(false);
   console.log("Disconnected");
 }
 
 function sendName() {
   var name = document.getElementById('name').value;
   stompClient.send("/app/hello", {}, JSON.stringify({
     'name' : name
   }));
 }

在瀏覽器中可以看到請求返回101狀態碼,意思就是切換協議

詳解java WebSocket的實現以及Spring WebSocket

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/akanairen/p/5616351.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 欧美激情亚洲 | 久操色 | 久久aⅴ乱码一区二区三区 一区二区精品视频 | 国产精品久久久久久久午夜片 | 国产黄色在线播放 | 在线精品国产 | 欧美成人一区二区 | 看亚洲a级一级毛片 | 国产精品一区二区三区四区 | 牛牛澡牛牛爽一区二区 | 精品91| av片免费 | 久久久久久久一区 | 久久99这里只有精品 | 久久久久久成人 | 国产精品久久久久无码av | 伊人网站 | 中文在线观看www | 久久不射电影网 | 国产女人爽到高潮免费视频 | 亚洲高清视频在线 | 中文字幕视频在线 | 久久一区 | 日韩国伦理久久一区 | 91亚洲日本| 天天操夜夜爽 | 国产欧美精品 | 亚洲精品久久久一区二区三区 | 国产一级片 | 国产精品一区二区视频 | 美女视频黄8频a美女大全 | 中文字幕亚洲欧美日韩在线不卡 | 淫片在线观看 | 久久中文视频 | 麻豆av一区 | 日本免费一区二区三区四区 | 日韩高清一区二区 | 亚洲激情在线 | 久久久国产精品 | 一级毛毛片 | 一区二区三区四区电影 |