国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架)

這篇具有很好參考價值的文章主要介紹了【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

最近我學(xué)習(xí)了WebSocket,為了更好地掌握這一技術(shù),我決定通過做一個項(xiàng)目來鞏固學(xué)習(xí)成果。在這個項(xiàng)目中,我將使用JavaScript和WebSocket來實(shí)現(xiàn)實(shí)時通信,讓客戶端和服務(wù)器端能夠?qū)崟r地傳遞和接收數(shù)據(jù)。通過這個項(xiàng)目,我希望能夠更深入地了解WebSocket的工作原理,并且能夠在實(shí)際應(yīng)用中靈活運(yùn)用這一技術(shù)。

1.技術(shù)棧

前端:vue3
后端:spring 框架

2.項(xiàng)目實(shí)現(xiàn)

1. 前端

1.項(xiàng)目初始化

這里使用vue ui 創(chuàng)建vue 項(xiàng)目,具體步驟可以參考這篇文章 Vue ui 初始化項(xiàng)目

2.項(xiàng)目目錄

自動生成的HelloWorld.vue文件可以刪除,這里只用創(chuàng)建一個Chat.vue文件【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架),websocket,前端,spring

3. 開發(fā)頁面

項(xiàng)目選擇了Ant Design來作為UI框架,直接去Ant Design官網(wǎng)上去拿一個自己看得上的組件即可。Ant Designg官網(wǎng)地址
進(jìn)入網(wǎng)站后第一件事情是選擇一個角色,本項(xiàng)目只提供了三個可選角色分別是
1.精神小伙
【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架),websocket,前端,spring
2.美女刺客【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架),websocket,前端,spring
3. 屌絲青年
【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架),websocket,前端,spring
這里我們通過一個走馬燈組件展示角色信息,直接在Ant Designg官網(wǎng)粘貼代碼即可

<template>
  <div class="body">
    <div class="character" v-if="isShow">

    <a-carousel class="img" arrows :after-change="onChange">
      <template #prevArrow>
        <div class="custom-slick-arrow" style="left: 10px; z-index: 1">
          <left-circle-outlined/>
        </div>
      </template>
      <template #nextArrow>
        <div class="custom-slick-arrow" style="right: 10px">
          <right-circle-outlined/>
        </div>
      </template>
      <div v-for="(item,i) in characterList" :key="i">
        <img :src="item.url" alt="" mode="scaleToFill">
      </div>
    </a-carousel>

    <div class="choose">
      <button class="btn" @click="setCharcter">選擇角色</button>
    </div>

  </div>
  </div>

</template>

<script setup>
import {LeftCircleOutlined, RightCircleOutlined} from '@ant-design/icons-vue';
import {ref, reactive} from 'vue'
import {message} from 'ant-design-vue';
//在線人數(shù)
var onlineNum = ref('0')

//在線列表
var onlineList = reactive([])
//是否顯示角色框
var isShow = ref(true)
var ws = null;
//角色的id
var id = ref(0)
//角色列表
var characterList = reactive(
    [
      {
        url: require("/public/1.jpg"),
        name: "精神小伙"
      },
      {
        url: require("/public/2.jpeg"),
        name: "美女刺客"
      },
      {
        url: require("/public/3.jpg"),
        name: "屌絲青年"
      }
    ]
)
//切換輪播圖后觸發(fā)
const onChange = (current) => {
  //得到當(dāng)前輪播圖的索引
  id.value = current
};
//設(shè)置角色
const setCharcter = () => {
  //選完角色后,要隱藏角色選擇框
  isShow.value = false

  //開啟webstocket服務(wù)的ip地址  ws:// + ip地址 + 訪問路徑
  ws = new WebSocket('ws://localhost:80/chat/' + id.value);

  //監(jiān)聽是否連接成功
  ws.onopen = function () {
    console.log('ws連接狀態(tài):' + ws.readyState)
    if (ws.readyState == 1) {
      message.success('歡迎來到西里網(wǎng)管內(nèi)部群');
    }
  }
// 接聽服務(wù)器發(fā)回的信息并處理展示
  ws.onmessage = function (data) {

    var res = JSON.parse(data.data);

    console.log(res)
    //type為0表示是系統(tǒng)發(fā)送的信息,為 1 時表示時用戶發(fā)來的信息
    if (res.type == 0) {
      //得到在線人數(shù)
      onlineNum.value = res.msg.length
      onlineList = res.msg
    } else {

      var msg = {
        content: "",
        type: '',
        id: ''
      }
      msg.type = '0'
      msg.msg = res.msg
      msg.id = res.id
      msgList.push(msg)
      console.log(res)
    }

  }

  ws.onclose = function () {
    // 監(jiān)聽整個過程中websocket的狀態(tài)
    console.log('ws連接狀態(tài):' + ws.readyState);
    ws.close();
  }
// 監(jiān)聽并處理error事件
  ws.onerror = function (error) {
    console.log(error);
  }
//監(jiān)聽窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時,主動去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會拋異常。
  window.onbeforeunload = function () {
    ws.close();
  }

}

</script>

<style scoped lang="less">
.body{
   color: #fff;
  font-weight: 900;
  letter-spacing: 2px;
  width: 100%;
  height: 100%;
  background-image: url("/public/5.gif");
  background-size: 50%;
  display: flex;
  align-items: center;
}

.character {
  border-radius: 10px;
  z-index: 10;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, .8);
  width: 300px;
  height: 400px;

  .img {
    width: 200px;
    height: 300px;
    margin: 10px auto;
    overflow: hidden;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;

    }
  }
    .btn {
    margin-top: 20px;
    background-color: dodgerblue;
    padding: 10px;
    border: transparent 1px solid;
    border-radius: 3px;
  }


}


:deep(.slick-slide) {
  text-align: center;
  height: 100%;
  line-height: 100%;
  background: #364d79;
  overflow: hidden;
}

:deep(.slick-arrow.custom-slick-arrow) {
  width: 25px;
  height: 25px;
  font-size: 25px;
  color: #fff;
  background-color: rgba(31, 45, 61, 0.11);
  transition: ease all 0.3s;
  opacity: 0.3;
  z-index: 1;
}

:deep(.slick-arrow.custom-slick-arrow:before) {
  display: none;
}

:deep(.slick-arrow.custom-slick-arrow:hover) {
  color: #fff;
  opacity: 0.5;
}

:deep(.slick-slide h3) {
  color: #fff;
}


</style>

運(yùn)行截圖
【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架),websocket,前端,spring
開發(fā)聊天框
js 代碼


//聊天框
const value = ref('');
const msgList = reactive([])
var sendMessage = (msg) => {
  if (msg.trim() !== '') {
    // 將消息發(fā)送到服務(wù)器
    ws.send(msg);
  }
}
const onSend = () => {
  sendMessage(value.value)
  var msg = {
    content: "",
    type: '',
    id: ''
  }
  //type為 1代表自己發(fā)的消息 type為 0代表別人發(fā)的消息
  msg.type = '1'
  msg.msg = value.value
  msgList.push(msg)
  console.log(msgList)
  value.value = ''

};

html

  <div class="container">
      <div class="left">
        <div class="top">
          在線人數(shù)
          <Icon type="ios-at-outline"/>
          <span>{{ onlineNum }}人</span>
        </div>
        <div class="list">
          <div class="item" v-for="(item,i) in onlineList" :key="i">
            <img :src="characterList[parseInt(item)].url" alt="" mode="scaleToFill">
            <span>{{ characterList[parseInt(item)].name }}</span>
          </div>

        </div>
      </div>
      <div class="right">
        <div class="top">
          西??網(wǎng)管
        </div>
        <div class="chat">
          <div v-for="(item,i) in msgList" :key="i" :class="item.type=='1'?'rightMsg':'leftMsg'">
            <img v-if="item.type=='0'" :src="characterList[parseInt(item.id)].url" alt="">
            <div class="msg">{{ item.msg }}</div>
            <img v-if="item.type=='1'" :src='characterList[parseInt(id)].url' alt="">
          </div>


        </div>
        <div class="bottom">
          <input
              v-model="value"
              placeholder="input  text"
          />
          <button @click="onSend">發(fā)送</button>

        </div>
      </div>
    </div>

css

.container {
  z-index: 1;
  border: solid 1px #2c3e50;
  width: 90%;
  height: 95%;
  margin: auto;
  display: flex;
  justify-content: center;

  .left {
    width: 20%;
    height: 100%;
    background-color: rgba(0, 0, 0, .5);

    .top {
      height: 30px;
      border-bottom: solid 1px #2c3e50;
      border-right: solid 1px #2c3e50;
      color: #fff;
      line-height: 30px;
      font-weight: bolder;
    }

    .item {
      height: 70px;
      display: flex;
      flex-direction: row;
      justify-content: start;
      align-items: center;
      width: 100%;

      border-bottom: 1px gray solid;

      img {
        width: 40px;
        height: 40px;
        border-radius: 20px;
        overflow: hidden;
        object-fit: cover;
        margin: 0 10px;

      }

      span {
        font-size: 14px;
        text-align: start;
      }
    }
  }

  .right {
    flex: 1;
    background-color: transparent !important;
    display: flex;
    flex-direction: column;

    .top {
      height: 70px;
      background-color: rgba(0, 0, 0, .5);
      width: 100%;
      font-size: 22px;
      text-align: center;
      line-height: 70px;
    }

    .chat {
      flex: 1;

      .leftMsg,
      .rightMsg {
        display: flex;
        flex-direction: row;
        justify-content: start;
        align-items: center;
        margin: 10px;

        img {
          width: 40px;
          height: 40px;
          border-radius: 20px;
          overflow: hidden;
          object-fit: cover;
          margin: 0 10px;
        }

        .msg {
          display: inline-block;
          padding: 10px;
          word-wrap: anywhere;
          max-width: 500px;
          background-color: #364d79;
          border-radius: 10px;
        }

      }

      .rightMsg {
        justify-content: end;

        .msg {
          color: black;
          background-color: white;
        }
      }
    }

    .bottom {
      height: 100px;
      display: flex;
      align-items: center;
      width: 80%;
      margin: 10px auto;

      input {
        width: 90%;
        border: none;
        outline: none;
        height: 40px;
        color: black;
        text-indent: 2px;
        line-height: 40px;
        border-radius: 10px 0 0 10px;

      }

      button {
        width: 10%;
        border: none;
        outline: none;
        height: 40px;
        line-height: 40px;
        border-radius: 0 10px 10px 0;
        background-color: dodgerblue;
      }

    }
  }
}

運(yùn)行結(jié)果
【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架),websocket,前端,spring

前端完整代碼
<template>
  <div class="body">
    <div class="container">
      <div class="left">
        <div class="top">
          在線人數(shù)
          <Icon type="ios-at-outline"/>
          <span>{{ onlineNum }}人</span>
        </div>
        <div class="list">
          <div class="item" v-for="(item,i) in onlineList" :key="i">
            <img :src="characterList[parseInt(item)].url" alt="" mode="scaleToFill">
            <span>{{ characterList[parseInt(item)].name }}</span>
          </div>

        </div>
      </div>
      <div class="right">
        <div class="top">
          西??網(wǎng)管
        </div>
        <div class="chat">
          <div v-for="(item,i) in msgList" :key="i" :class="item.type=='1'?'rightMsg':'leftMsg'">
            <img v-if="item.type=='0'" :src="characterList[parseInt(item.id)].url" alt="">
            <div class="msg">{{ item.msg }}</div>
            <img v-if="item.type=='1'" :src='characterList[parseInt(id)].url' alt="">
          </div>


        </div>
        <div class="bottom">
          <input
              v-model="value"
              placeholder="input  text"
          />
          <button @click="onSend">發(fā)送</button>

        </div>
      </div>
    </div>

    <div class="character" v-if="isShow">

      <a-carousel class="img" arrows :after-change="onChange">
        <template #prevArrow>
          <div class="custom-slick-arrow" style="left: 10px; z-index: 1">
            <left-circle-outlined/>
          </div>
        </template>
        <template #nextArrow>
          <div class="custom-slick-arrow" style="right: 10px">
            <right-circle-outlined/>
          </div>
        </template>
        <div v-for="(item,i) in characterList" :key="i">
          <img :src="item.url" alt="" mode="scaleToFill">
        </div>
      </a-carousel>

      <div class="choose">
        <button class="btn" @click="setCharcter">選擇角色</button>
      </div>

    </div>
  </div>

</template>

<script setup>
import {LeftCircleOutlined, RightCircleOutlined} from '@ant-design/icons-vue';
import {ref, reactive} from 'vue'
import {message} from 'ant-design-vue';
//在線人數(shù)
var onlineNum = ref('0')

//在線列表
var onlineList = reactive([])
//是否顯示角色框
var isShow = ref(true)
var ws = null;
//角色的id
var id = ref(0)
//角色列表
var characterList = reactive(
    [
      {
        url: require("/public/1.jpg"),
        name: "精神小伙"
      },
      {
        url: require("/public/2.jpeg"),
        name: "美女刺客"
      },
      {
        url: require("/public/3.jpg"),
        name: "屌絲青年"
      }
    ]
)
//切換輪播圖后觸發(fā)
const onChange = (current) => {
  //得到當(dāng)前輪播圖的索引
  id.value = current
};
//設(shè)置角色
const setCharcter = () => {
  //選完角色后,要隱藏角色選擇框
  isShow.value = false

  //開啟webstocket服務(wù)的ip地址  ws:// + ip地址 + 訪問路徑
  ws = new WebSocket('ws://localhost:80/chat/' + id.value);

  //監(jiān)聽是否連接成功
  ws.onopen = function () {
    console.log('ws連接狀態(tài):' + ws.readyState)
    if (ws.readyState == 1) {
      message.success('歡迎來到西里網(wǎng)管內(nèi)部群');
    }
  }
// 接聽服務(wù)器發(fā)回的信息并處理展示
  ws.onmessage = function (data) {

    var res = JSON.parse(data.data);

    console.log(res)
    //type為0表示是系統(tǒng)發(fā)送的信息,為 1 時表示時用戶發(fā)來的信息
    if (res.type == 0) {
      //得到在線人數(shù)
      onlineNum.value = res.msg.length
      onlineList = res.msg
    } else {

      var msg = {
        content: "",
        type: '',
        id: ''
      }
      msg.type = '0'
      msg.msg = res.msg
      msg.id = res.id
      msgList.push(msg)
      console.log(res)
    }

  }

  ws.onclose = function () {
    // 監(jiān)聽整個過程中websocket的狀態(tài)
    console.log('ws連接狀態(tài):' + ws.readyState);
    ws.close();
  }
// 監(jiān)聽并處理error事件
  ws.onerror = function (error) {
    console.log(error);
  }
//監(jiān)聽窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時,主動去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會拋異常。
  window.onbeforeunload = function () {
    ws.close();
  }

}

//聊天框
const value = ref('');
const msgList = reactive([])
var sendMessage = (msg) => {
  if (msg.trim() !== '') {
    // 將消息發(fā)送到服務(wù)器
    ws.send(msg);
  }
}
const onSend = () => {
  sendMessage(value.value)
  var msg = {
    content: "",
    type: '',
    id: ''
  }
  //type為 1代表自己發(fā)的消息 type為 0代表別人發(fā)的消息
  msg.type = '1'
  msg.msg = value.value
  msgList.push(msg)
  console.log(msgList)
  value.value = ''

};

</script>

<style scoped lang="less">
.body {
  color: #fff;
  font-weight: 900;
  letter-spacing: 2px;
  width: 100%;
  height: 100%;
  background-image: url("/public/5.gif");
  background-size: 50%;
  display: flex;
  align-items: center;
  position: relative;
}

.character {
  border-radius: 10px;
  z-index: 10;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, .8);
  width: 300px;
  height: 400px;

  .img {
    width: 200px;
    height: 300px;
    margin: 10px auto;
    overflow: hidden;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;

    }
  }

  .btn {
    margin-top: 20px;
    background-color: dodgerblue;
    padding: 10px;
    border: transparent 1px solid;
    border-radius: 3px;
  }


}

.body {
  :deep(.slick-slide) {
    text-align: center;
    height: 100%;
    line-height: 100%;
    background: #364d79;
    overflow: hidden;
  }

  :deep(.slick-arrow.custom-slick-arrow) {
    width: 25px;
    height: 25px;
    font-size: 25px;
    color: #fff;
    background-color: rgba(31, 45, 61, 0.11);
    transition: ease all 0.3s;
    opacity: 0.3;
    z-index: 1;
  }

  :deep(.slick-arrow.custom-slick-arrow:before) {
    display: none;
  }

  :deep(.slick-arrow.custom-slick-arrow:hover) {
    color: #fff;
    opacity: 0.5;
  }

  :deep(.slick-slide h3) {
    color: #fff;
  }

}

.container {
  z-index: 1;
  border: solid 1px #2c3e50;
  width: 90%;
  height: 95%;
  margin: auto;
  display: flex;
  justify-content: center;

  .left {
    width: 20%;
    height: 100%;
    background-color: rgba(0, 0, 0, .5);

    .top {
      height: 30px;
      border-bottom: solid 1px #2c3e50;
      border-right: solid 1px #2c3e50;
      color: #fff;
      line-height: 30px;
      font-weight: bolder;
    }

    .item {
      height: 70px;
      display: flex;
      flex-direction: row;
      justify-content: start;
      align-items: center;
      width: 100%;

      border-bottom: 1px gray solid;

      img {
        width: 40px;
        height: 40px;
        border-radius: 20px;
        overflow: hidden;
        object-fit: cover;
        margin: 0 10px;

      }

      span {
        font-size: 14px;
        text-align: start;
      }
    }
  }

  .right {
    flex: 1;
    background-color: transparent !important;
    display: flex;
    flex-direction: column;

    .top {
      height: 70px;
      background-color: rgba(0, 0, 0, .5);
      width: 100%;
      font-size: 22px;
      text-align: center;
      line-height: 70px;
    }

    .chat {
      flex: 1;

      .leftMsg,
      .rightMsg {
        display: flex;
        flex-direction: row;
        justify-content: start;
        align-items: center;
        margin: 10px;

        img {
          width: 40px;
          height: 40px;
          border-radius: 20px;
          overflow: hidden;
          object-fit: cover;
          margin: 0 10px;
        }

        .msg {
          display: inline-block;
          padding: 10px;
          word-wrap: anywhere;
          max-width: 500px;
          background-color: #364d79;
          border-radius: 10px;
        }

      }

      .rightMsg {
        justify-content: end;

        .msg {
          color: black;
          background-color: white;
        }
      }
    }

    .bottom {
      height: 100px;
      display: flex;
      align-items: center;
      width: 80%;
      margin: 10px auto;

      input {
        width: 90%;
        border: none;
        outline: none;
        height: 40px;
        color: black;
        text-indent: 2px;
        line-height: 40px;
        border-radius: 10px 0 0 10px;

      }

      button {
        width: 10%;
        border: none;
        outline: none;
        height: 40px;
        line-height: 40px;
        border-radius: 0 10px 10px 0;
        background-color: dodgerblue;
      }

    }
  }
}


</style>

2. 后端部分

  1. 導(dǎo)入maven坐標(biāo)
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

3). 定義WebSocket服務(wù)端組件

package com.example.chat;

/**
 * @author 余煒
 * @version 1.0
 */

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ServerEndpoint 注解是一個類層次的注解,它的功能主要是將目前的類定義成一個websocket服務(wù)器端,
 * 注解的值將被用于監(jiān)聽用戶連接的終端訪問URL地址,客戶端可以通過這個URL來連接到WebSocket服務(wù)器端
 */
@ServerEndpoint("/chat/{id}")
@Component
@Slf4j
public class Websocket {

    //記錄連接的客戶端
    public static Map<String, Session> clients = new ConcurrentHashMap<>();

    //0:連接成功系統(tǒng)發(fā)送信息 1:用戶發(fā)送的消息
    @OnOpen
    public void onOpen(Session session, @PathParam("id") String id) {

        clients.put(id, session);
        //獲取在線用戶的id
        List<String> onlineList = new ArrayList<>();
        for (String key : clients.keySet()) {
            onlineList.add(key);
        }
        Websocket.sendMessage(new response<List<String>>(0, id, onlineList), session);
    }

    /**
     * 連接關(guān)閉調(diào)用的方法
     */
    @OnClose
    public void onClose(@PathParam("id") String id) {
        log.info(id + "連接斷開!");
        clients.remove(id);
    }

    /**
     * 判斷是否連接的方法
     *
     * @return
     */
    public static boolean isServerClose() {
        if (Websocket.clients.values().size() == 0) {
            log.info("已斷開");
            return true;
        } else {
            log.info("已連接");
            return false;
        }
    }


    /**
     * 發(fā)送給所有用戶
     *
     * @param
     */
    public static void sendMessage(response response, Session session) {
        String message = JSONObject.toJSONString(response);
        for (Session session1 : Websocket.clients.values()) {
            try {
                 //判斷一下是不是系統(tǒng)發(fā)送的信息,和是不是自己發(fā)送的信息
                if (response.getType() == 0 || !session.equals(session1))
                    session1.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 收到客戶端消息后調(diào)用的方法
     *
     * @param message
     * @param session
     */

    @OnMessage
    public void onMessage(String message, Session session, @PathParam("id") String id) {

        sendMessage(new response<String>(1, id, message), session);


    }

    /**
     * 發(fā)生錯誤時的回調(diào)函數(shù)
     *
     * @param error
     */
    @OnError
    public void onError(Throwable error) {
        log.info("錯誤");
        error.printStackTrace();
    }

}

4). 定義配置類,注冊WebSocket的服務(wù)端組件

package com.example.chat.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * @author 余煒
 * @version 1.0
 */

/**
 * 開啟WebSocket支持
 */
@Configuration
public class webSocketConfig {
   @Bean
   public ServerEndpointExporter serverEndpointExporter() {
      return new ServerEndpointExporter();
   }
}

封裝一個回復(fù)實(shí)體類

package com.example.chat;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author 余煒
 * @version 1.0
 */
@Data
@AllArgsConstructor
public class response<T> {
   private int type;

   private String id;

   private  T msg;


}

結(jié)語

本次項(xiàng)目實(shí)戰(zhàn)分享到此圓滿結(jié)速,希望能對那些正在探索或已經(jīng)踏入WebSocket領(lǐng)域的讀者們有所幫助。我希望這篇博客能激發(fā)你的興趣,讓你對WebSocket有更深的理解,同時也希望你能在評論區(qū)分享你的經(jīng)驗(yàn)和觀點(diǎn),讓我們一起學(xué)習(xí),一起進(jìn)步。再次感謝你閱讀我的博客,期待你的反饋和參與!讓我們一起在學(xué)習(xí)的道路上一起前行,共同探索技術(shù)的無盡可能。文章來源地址http://www.zghlxwxcb.cn/news/detail-757466.html

到了這里,關(guān)于【W(wǎng)ebSocket項(xiàng)目實(shí)戰(zhàn)】聊天室(前端vue3、后端spring框架)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Spring boot 項(xiàng)目(二十三)——用 Netty+Websocket實(shí)現(xiàn)聊天室

    Spring boot 項(xiàng)目(二十三)——用 Netty+Websocket實(shí)現(xiàn)聊天室

    Netty 是基于 Java NIO 的異步事件驅(qū)動的網(wǎng)絡(luò)應(yīng)用框架,使用 Netty 可以快速開發(fā)網(wǎng)絡(luò)應(yīng)用,Netty 提供了高層次的抽象來簡化 TCP 和 UDP 服務(wù)器的編程,但是你仍然可以使用底層的 API。 Netty 的內(nèi)部實(shí)現(xiàn)是很復(fù)雜的,但是 Netty 提供了簡單易用的API從網(wǎng)絡(luò)處理代碼中解耦業(yè)務(wù)邏輯。

    2023年04月15日
    瀏覽(88)
  • 項(xiàng)目介紹:《Online ChatRoom》網(wǎng)頁聊天室 — Spring Boot、MyBatis、MySQL和WebSocket的奇妙融合

    項(xiàng)目介紹:《Online ChatRoom》網(wǎng)頁聊天室 — Spring Boot、MyBatis、MySQL和WebSocket的奇妙融合

    在當(dāng)今數(shù)字化社會,即時通訊已成為人們生活中不可或缺的一部分。為了滿足這一需求,我開發(fā)了一個名為\\\"WeTalk\\\"的聊天室項(xiàng)目,該項(xiàng)目基于Spring Boot、MyBatis、MySQL和WebSocket技術(shù),為用戶提供了一個實(shí)時交流的平臺。在本篇博客中,我將介紹該項(xiàng)目的設(shè)計和實(shí)現(xiàn),以及其在社交

    2024年02月11日
    瀏覽(1210)
  • websocket網(wǎng)頁聊天室

    實(shí)現(xiàn)websocket網(wǎng)頁聊天室可以遵循以下步驟: 創(chuàng)建一個基于瀏覽器的WebSocket客戶端,使用JavaScript??梢允褂肏TML5的WebSocket API。 編寫服務(wù)器端的WebSocket應(yīng)用程序,可以使用Node.js和WebSocket模塊。 在服務(wù)器端創(chuàng)建一個WebSocket服務(wù)器,監(jiān)聽客戶端請求,并將客戶端與服務(wù)器端連接起

    2024年02月06日
    瀏覽(95)
  • Django實(shí)現(xiàn)websocket聊天室

    WebSocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議。它實(shí)現(xiàn)了瀏覽器與服務(wù)器雙向通信,即允許服務(wù)器主動發(fā)送信息給客戶端。因此,在WebSocket中,瀏覽器和服務(wù)器只需要完成一次握手,兩者之間就直接可以創(chuàng)建持久性的連接,并進(jìn)行雙向數(shù)據(jù)傳輸,客戶端和服務(wù)器之間的數(shù)據(jù)交換變

    2023年04月23日
    瀏覽(92)
  • 【W(wǎng)ebSocket】SpringBoot整合WebSocket實(shí)現(xiàn)聊天室(一)

    【W(wǎng)ebSocket】SpringBoot整合WebSocket實(shí)現(xiàn)聊天室(一)

    目錄 一、準(zhǔn)備 1、引入依賴 2、創(chuàng)建配置類 二、相關(guān)注解 首先我們需要在項(xiàng)目中引入依賴,有兩種方式。第一種我們可以在創(chuàng)建Spring Boot項(xiàng)目時搜索WebSocket然后勾選依賴 第二種是我們可以直接在項(xiàng)目的pom.xml文件中插入以下依賴 我們需要進(jìn)行如下配置 ServerEndpointExporter 是一個

    2024年02月13日
    瀏覽(92)
  • 基于WebSocket的在線文字聊天室

    基于WebSocket的在線文字聊天室

    與Ajax不同,WebSocket可以使服務(wù)端主動向客戶發(fā)送響應(yīng),本案例就是基于WebSocket的一個在線聊天室,不過功能比較簡單,只能滿足文字交流。演示如下。 案例學(xué)習(xí)于b站up主,鏈接?。這位up主講的非常清楚,值得去學(xué)習(xí)。本文屬于記錄自我學(xué)習(xí)過程的文章。 項(xiàng)目目錄下app.js 項(xiàng)

    2024年02月13日
    瀏覽(34)
  • django websocket實(shí)現(xiàn)聊天室功能

    django websocket實(shí)現(xiàn)聊天室功能

    注意事項(xiàng)channel版本 django2.x 需要匹配安裝 channels 2 django3.x 需要匹配安裝 channels 3 Django 3.2.4 channels 3.0.3 Django 3.2.* channels 3.0.2 Django4.2 channles==3.0.5 是因?yàn)樽钚掳鎐hannels默認(rèn)不帶daphne服務(wù)器 直接用命令 python manage.py runsever 默認(rèn)運(yùn)行的是wsgi ,修改,刪除settings中的wsgi,都不能正確運(yùn)

    2024年01月22日
    瀏覽(27)
  • Java+Vue實(shí)現(xiàn)聊天室(WebSocket進(jìn)階-聊天記錄)

    Java+Vue實(shí)現(xiàn)聊天室(WebSocket進(jìn)階-聊天記錄)

    WebSocket 是一種在單個TCP連接上進(jìn)行全雙工通信的協(xié)議。WebSocket通信協(xié)議于2011年被IETF定為標(biāo)準(zhǔn)RFC 6455,并由RFC7936補(bǔ)充規(guī)范。WebSocket API也被W3C定為標(biāo)準(zhǔn)。 WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù)。在WebSocket API中,瀏覽器和服

    2024年02月11日
    瀏覽(111)
  • springboot+websocket實(shí)現(xiàn)簡單的聊天室

    springboot+websocket實(shí)現(xiàn)簡單的聊天室

    HTML HTML是創(chuàng)建和構(gòu)造網(wǎng)頁的標(biāo)準(zhǔn)標(biāo)記語言。它使用一組標(biāo)記標(biāo)簽描述網(wǎng)頁上的內(nèi)容結(jié)構(gòu)。HTML文檔由HTML元素的嵌套結(jié)構(gòu)組成,每個元素由尖括號( )括起的標(biāo)簽表示。這些元素定義了網(wǎng)頁的各個部分,如標(biāo)題、段落、圖像、鏈接、表單等。 JavaScript JavaScript是一種高級、解釋性

    2024年01月21日
    瀏覽(23)
  • 在線聊天室(Vue+Springboot+WebSocket)

    在線聊天室(Vue+Springboot+WebSocket)

    實(shí)現(xiàn)了一個簡單的在線聊天室的前后端。前端用Vue實(shí)現(xiàn),后端用Springboot實(shí)現(xiàn)。 ????????在線聊天室的功能包括創(chuàng)建用戶和顯示在線用戶列表、發(fā)送消息和顯示消息列表、用戶和消息列表實(shí)時更新這幾點(diǎn)。以下是整體功能的活動圖: 用戶身份 ? ? ? ? 進(jìn)入聊天室的用戶需

    2024年01月15日
    瀏覽(28)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包