欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot基于websocket實(shí)現(xiàn)簡(jiǎn)單在線聊天功能

 更新時(shí)間:2020年06月24日 10:54:29   作者:yytxdy  
這篇文章主要介紹了Springboot基于websocket實(shí)現(xiàn)簡(jiǎn)單在線聊天功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

添加maven依賴(lài)

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>springboot-websocket</artifactId>
  <name>springboot-websocket</name>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
  </dependencies>

</project>

添加websocket配置

@Configuration
@EnableWebSocket
public class MyWebSocketConfig implements WebSocketConfigurer {
  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("*");
  }

  @Bean
  public WebSocketHandler myHandler() {
    return new MyTextWebSocketHandler();
  }
}

實(shí)現(xiàn)具體的handler

public class MyTextWebSocketHandler extends TextWebSocketHandler {
  private Set<WebSocketSession> sessions = new HashSet<>();

  @Override
  protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    if (session.isOpen()) {
      sessions.add(session);
    }
    sendToAll(message);
  }
  private void sendToAll(TextMessage message) throws IOException {
    for (WebSocketSession session : sessions) {
      session.sendMessage(message);
    }
  }
  @Override
  public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    sessions.remove(session);
  }
}

即可通過(guò)ws://localhost:8080/myHandler訪問(wèn)websocket

添加測(cè)試頁(yè)面:

<html>
<script type="text/javascript">
  if ("WebSocket" in window) {
    var ws = new WebSocket("ws://localhost:8080/myHandler");
    ws.onopen = function () {

    };

    ws.onmessage = function (evt) {
      document.getElementById('messageDiv').innerHTML += evt.data + "</br>";
    };

    ws.onclose = function () {
      console.log("close connect");
    };
  } else {
    alert("您的瀏覽器不支持 WebSocket!");
  }

  function send() {
    ws.send(document.getElementById("input").value + ": " + document.getElementById("message").value);
  }
</script>

</head>
<body>
當(dāng)前用戶: <input id="input"/><br/>
<a href="#" rel="external nofollow" onclick="send();">發(fā)送消息</a>: <input id="message"/>
<div id="messageDiv"></div>
</body>
</html>

即可實(shí)現(xiàn)簡(jiǎn)單的通信功能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入理解springboot中配置文件application.properties

    深入理解springboot中配置文件application.properties

    本文主要介紹了springboot中配置文件application.properties,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Spring中bean的繼承與抽象代碼示例

    Spring中bean的繼承與抽象代碼示例

    這篇文章主要介紹了Spring中bean的繼承與抽象代碼示例,涉及abstract 屬性,bean實(shí)例化,子bean 與普通bean等相關(guān)內(nèi)容,代碼示例中注釋比較詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • Java使用遞歸回溯完美解決八皇后的問(wèn)題

    Java使用遞歸回溯完美解決八皇后的問(wèn)題

    這篇文章主要介紹了Java基于循環(huán)遞歸回溯實(shí)現(xiàn)八皇后問(wèn)題算法,結(jié)合具體實(shí)例形式分析了java的遍歷、遞歸、回溯等算法實(shí)現(xiàn)八皇后問(wèn)題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2021-11-11
  • 關(guān)于Integer.parseInt()方法的使用

    關(guān)于Integer.parseInt()方法的使用

    這篇文章主要介紹了關(guān)于Integer.parseInt()方法的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Installij IDEA install或clean項(xiàng)目的使用

    Installij IDEA install或clean項(xiàng)目的使用

    這篇文章主要介紹了Installij IDEA install或clean項(xiàng)目的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • JAVA使用DBUtils操作數(shù)據(jù)庫(kù)

    JAVA使用DBUtils操作數(shù)據(jù)庫(kù)

    這篇文章主要介紹了JAVA使用DBUtils操作數(shù)據(jù)庫(kù)的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家學(xué)習(xí)JAVA,感興趣的朋友可以了解下
    2020-07-07
  • java中Socket設(shè)置超時(shí)時(shí)間的兩種方式

    java中Socket設(shè)置超時(shí)時(shí)間的兩種方式

    這篇文章主要介紹了java中Socket設(shè)置超時(shí)時(shí)間的兩種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java保留兩位小數(shù)的實(shí)現(xiàn)方法

    Java保留兩位小數(shù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了 Java保留兩位小數(shù)的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 淺談Maven的build生命周期和常用plugin

    淺談Maven的build生命周期和常用plugin

    Maven和gradle應(yīng)該是現(xiàn)代java程序員中使用的最多的兩種構(gòu)建工具。在它們出現(xiàn)之前,則是ant的天下。本文將介紹Maven的build生命周期和常用plugin。
    2021-06-06
  • Java inputstream和outputstream使用詳解

    Java inputstream和outputstream使用詳解

    這篇文章主要介紹了Java inputstream和outputstream使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評(píng)論