vue+websocket,实现简易聊天室

1.安装vue-cli
2.init 初始化项目
3.npm install ws(websocket)
4.文件夹中创建server.js

var userNum = 0 // 统计人数
var chatList = []// 记录聊天内容
var WebSocketServer = require('ws').Server// 引入 ws
// 端口对应
var wss = new WebSocketServer({ port: 8080 })
// 调用broadcast广播,实现数据互通和实时更新
wss.broadcast = function (msg) {
  wss.clients.forEach(function (item) {
    item.send(msg)
  })
}
// 监听连接
wss.on('connection', function (ws) {
  userNum++
  wss.broadcast(JSON.stringify({ funName: 'userCount', users: userNum, chat: chatList })) // 建立连接成功广播一次当前在线人数
  console.log('连接人数:', userNum)
  // 接受前端发来消息
  ws.on('message', function (e) {
    var resData = JSON.parse(e) // 消息处理 字符串转为对象
    console.log('接收到来自clent的消息:' + resData.msg)
    // 每次发送信息,都会把信息存起来,然后通过广播传递出去,这样此每次进来的用户就能看到之前的数据
    chatList.push({ userId: resData.userId, content: resData.msg })
    // 每次发送都相当于广播一次消息
    wss.broadcast(JSON.stringify({ userId: resData.userId, msg: resData.msg }))
  })
  ws.on('close', function (e) {
    userNum--
    // 建立连接关闭广播一次当前在线人数
    wss.broadcast(JSON.stringify({ funName: 'userCount', users: userNum, chat: chatList }))
    console.log('Connected clients:', userNum)
    console.log('长连接已关闭')
  })
})
console.log('服务器创建成功')

在这里插入图片描述

<template>
  <div class="chat-box">
    <div class="header">聊天室人数:{{count}}</div>
    <scroll class="message" ref="scorll" :data="list">
      <div>
        <div class="msg-box" ref="msgbox" v-for="(item,index) in list" :key="index">
          <span>{{item.content}}</span>
          <div class="header-avtor"></div>
        </div>
      </div>
    </scroll>
    <div class="bottom">
      <div class="down">
        <input type="text" ref="sendMsg" v-model="contentText" />
        <button ref="btn" @click="sendText">发送</button>
      </div>
    </div>
  </div>
</template>
<script>
import Scroll from 'base/scroll/scroll'
export default {
  data () {
    return {
      ws: null,
      count: 0,
      userId: null,
      list: [],
      contentText: '' // input输入的值
    }
  },
  created () {
    this.getUserId()
  },
  mounted () {
    this.initWebsocket()
    this.$refs.sendMsg.focus()
  },
  updated () {
  },
  methods: {
    getUserId () {
      let time = new Date().getTime()
      this.userId = time
    },
    // 滚动到底部
    scrollBottm () {
      let el = this.$refs.msgbox
      el.scrollTop = el.scrollHeight
      console.info(el.scrollHeight)
    },
    // 发送聊天消息
    sendText () {
      let _this = this
      _this.$refs.sendMsg.focus()
      if (!_this.contentText) {
        return false
      }
      let params = {
        userId: _this.userId,
        msg: _this.contentText
      }
      if (_this.ws.readyState == 1) {
        _this.ws.send(JSON.stringify(params)) // 调用WebSocket send()发送信息的方法
      }

      _this.contentText = ''
    },
    initWebsocket () {
      let _this = this
      // 判断页面有没有存在websocket连接
      if (window.WebSocket) {
        let ws = new WebSocket('ws://127.0.0.1:8080')
        _this.ws = ws
        console.log(ws)
        ws.onopen = function (e) {
          console.log('服务器连接成功')
        }
        ws.onclose = function () {
          console.log('服务器连接关闭')
        }
        ws.onerror = function () {
          console.log('服务器连接出错')
        }
        ws.onmessage = function (e) {
          // 接收服务器返回的数据
          let resData = JSON.parse(e.data)
          if (resData.funName === 'userCount') {
            _this.count = resData.users
            _this.list = resData.chat
            console.log(_this.list, '1234')
          } else {
            _this.list = [
              ..._this.list,
              { userId: resData.userId, content: resData.msg }
            ]
          }
        }
      }
    }
  },
  components: {
    Scroll
  },
  watch: {
    list () {
      this.$refs.scorll.refresh()
    }
  }
}
</script>
<style scoped>
.message {
  padding-top: 60px;
  margin-bottom: 60px;
  height: 100%;
  overflow: hidden;
}
.header {
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background: blue;
  color: #fff;
  font-size: 18px;
  position: fixed;
  top: 0;
  z-index: 2;
}
.bottom {
  position: fixed;
  bottom: 0;
  width: 100%;
  border-top: 1px solid #ccc;
}
.down {
  display: flex;
  align-items: center;
  padding: 5px;
  background: #fff;
}
.bottom input {
  flex: 1;
  height: 40px;
  font-display: 18px;
}
.bottom button {
  border: none;
  width: 70px;
  height: 44px;
  background: blue;
  color: #fff;
  line-height: 40px;
  text-align: center;
  border-radius: 5px;
  margin-left: 10px;
}
.msg-box {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  margin: 20px 10px 0px 0px;
}

.avtive {
  display: inline-block;
  background: blue;
  color: #fff;
  padding: 10px;
  font-size: 16px;
  border-radius: 6px;
  margin-right: 10px;
  flex-wrap: wrap;
  word-break: break-all;
  max-width: 220px;
}
.header-avtor {
  width: 40px !important;
  height: 40px;
  border-radius: 50%;
  background: #ccc;
}
.avtive-left {
  justify-content: flex-start;
}
</style>

node server.js 启动服务端
运行npm run dev即可
参考文章:https://www.jianshu.com/p/6e52493c3bae

  • 4
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的使用Spring Boot、Vue.js和WebSocket实现聊天室的代码示例: Spring Boot后端代码: ```java @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(new WebSocketHandler(), "/chat").setAllowedOrigins("*"); } @Bean public ObjectMapper objectMapper() { return new ObjectMapper(); } } class WebSocketHandler extends TextWebSocketHandler { private static final Map<WebSocketSession, String> users = new ConcurrentHashMap<>(); @Override public void afterConnectionEstablished(WebSocketSession session) { users.put(session, "Anonymous"); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { ChatMessage chatMessage = new ObjectMapper().readValue(message.getPayload(), ChatMessage.class); if (chatMessage.getType() == ChatMessage.MessageType.JOIN) { users.put(session, chatMessage.getSender()); } for (WebSocketSession user : users.keySet()) { user.sendMessage(new TextMessage(new ObjectMapper().writeValueAsString(chatMessage))); } } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { users.remove(session); } } @Data @AllArgsConstructor @NoArgsConstructor class ChatMessage { public enum MessageType { CHAT, JOIN, LEAVE } private String sender; private String content; private MessageType type; public static ChatMessage joinMessage(String sender) { return new ChatMessage(sender, "", MessageType.JOIN); } public static ChatMessage leaveMessage(String sender) { return new ChatMessage(sender, "", MessageType.LEAVE); } } @RestController public class ChatController { @GetMapping("/users") public List<String> users() { return new ArrayList<>(WebSocketHandler.users.values()); } } ``` Vue.js前端代码: ```html <template> <div> <h2>Chat Room</h2> <div> <label>Your name:</label> <input v-model="name" @keyup.enter="join" /> <button @click="join">Join</button> </div> <div v-if="joined"> <div> <label>Message:</label> <input v-model="message" @keyup.enter="send" /> <button @click="send">Send</button> </div> <div> <h3>Users:</h3> <ul> <li v-for="user in users" :key="user">{{ user }}</li> </ul> </div> <div> <h3>Chat:</h3> <ul> <li v-for="chat in chats" :key="chat.id"> <strong>{{ chat.sender }}:</strong> {{ chat.content }} </li> </ul> </div> </div> </div> </template> <script> import SockJS from "sockjs-client"; import Stomp from "stompjs"; export default { data() { return { name: "", message: "", joined: false, chats: [], users: [], stompClient: null, }; }, methods: { join() { const socket = new SockJS("/chat"); this.stompClient = Stomp.over(socket); this.stompClient.connect({}, () => { this.stompClient.subscribe("/topic/chat", (message) => { const chat = JSON.parse(message.body); if (chat.type === "JOIN") { this.users.push(chat.sender); } else if (chat.type === "LEAVE") { this.users.splice(this.users.indexOf(chat.sender), 1); } this.chats.push(chat); }); this.stompClient.send( "/app/chat", JSON.stringify(ChatMessage.joinMessage(this.name)) ); this.joined = true; }); }, send() { this.stompClient.send( "/app/chat", JSON.stringify( new ChatMessage(this.name, this.message, ChatMessage.MessageType.CHAT) ) ); this.message = ""; }, }, }; class ChatMessage { static MessageType = { CHAT: "CHAT", JOIN: "JOIN", LEAVE: "LEAVE", }; constructor(sender, content, type) { this.sender = sender; this.content = content; this.type = type; } static joinMessage(sender) { return new ChatMessage(sender, "", ChatMessage.MessageType.JOIN); } static leaveMessage(sender) { return new ChatMessage(sender, "", ChatMessage.MessageType.LEAVE); } } </script> ``` 在这个示例中,我们使用了Spring Boot的WebSocket支持来处理来自客户端的事件。我们创建了一个WebSocket处理程序,它维护了一个用户会话列表,并在用户加入、离开或发送聊天消息时广播消息到所有连接的客户端。我们还为WebSocket处理程序创建了一个控制器,以便在客户端请求所有当前连接的用户列表时返回它们。 在Vue.js应用程序中,我们使用SockJS和Stomp.js来建立与服务器的WebSocket连接,并处理来自服务器的事件。我们使用Vue.js的数据绑定来更新聊天消息、用户列表和用户输入框中的数据,并在加入聊天室、发送聊天消息或断开连接时发送相关的WebSocket事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值