Nest.js实现一个简单的聊天室

server/2024/10/18 8:23:07/

本文将介绍如何使用 Nest.jsUni-app 实现一个简单的实时聊天应用。后端使用 @nestjs/websocketssocket.io,前端使用 uni-app 并集成 socket.io-client。这个项目允许多个用户同时加入聊天并实时交换消息。

效果图:

一、准备工作
  1. 安装 Node.js 和 npm
  2. 全局安装 Nest.js CLI
npm install -g @nestjs/cli
二、后端:Nest.js 实现 WebSocket 服务
1. 创建 Nest.js 项目

首先使用 CLI 创建一个新的 Nest.js 项目:

nest new nest-chat

选择使用 npmyarn 进行依赖管理。

2. 安装 WebSocket 和 Socket.io 相关依赖

在项目中,安装 WebSocket 和 socket.io 相关的依赖包:

npm install @nestjs/websockets socket.io
3. 创建 WebSocket 网关

src 目录下创建一个 chat 模块,并在该模块中创建 WebSocket 网关:

nest g module chat
nest g gateway chat/chat

这将生成一个 WebSocket 网关类。修改 chat.gateway.ts 文件,添加基本的 WebSocket 聊天功能:


import {SubscribeMessage,WebSocketGateway,OnGatewayInit,WebSocketServer,OnGatewayConnection,OnGatewayDisconnect,} from '@nestjs/websockets';import { Logger } from '@nestjs/common';import { Socket, Server } from 'socket.io';@WebSocketGateway({namespace: 'chat',cors: {origin: '*',},})export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {@WebSocketServer() server: Server;private logger: Logger = new Logger('ChatGateway');users = 0;@SubscribeMessage('msgToServer')handleMessage(client: Socket, payload: string): void {// 获取当前时间并格式化为“YYYY-MM-DD HH:mm:ss”const currentTime = new Date().toLocaleString('zh-CN', {year: 'numeric',month: '2-digit',day: '2-digit',hour: '2-digit',minute: '2-digit',second: '2-digit',hour12: false, // 使用24小时制}).replace(/\//g, '-').replace(/,/, ' '); // 替换分隔符以符合所需格式// 创建一个新的消息对象,包含时间和消息内容const messageWithTime = {time: currentTime, // 当前时间data: payload,};this.server.emit('msgToClient', messageWithTime); // 发送包含时间的消息对象}afterInit(server: Server) {this.logger.log('Init');}handleDisconnect(client: Socket) {this.logger.log(`Client disconnected: ${client.id}`);this.users--;// 通知连接的客户端当前用户数量this.server.emit('users', this.users);}handleConnection(client: Socket, ...args: any[]) {this.logger.log(`Client connected: ${client.id}`);this.users++;// 通知连接的客户端当前用户数量this.server.emit('users', this.users);}}
4. 将 WebSocket 网关注册到模块中

chat.module.ts 中,将 ChatGateway 加入到模块的 providers 中:

import { Module } from '@nestjs/common';
import { ChatGateway } from './chat.gateway';
//cli: nest g module chat
@Module({providers: [ChatGateway],
})
export class ChatModule {}
5. 在主模块中导入 ChatModule

打开 app.module.ts,并导入 ChatModule

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ChatModule } from './chat/chat.module';
@Module({imports: [ChatModule ],controllers: [AppController],providers: [AppService],
})
export class AppModule {}

至此,后端部分已经完成,接下来启动 Nest.js 服务: 

npm run start

三、前端:Uni-App 实现客户端

1. 安装 socket.io-client

在 Uni-App 项目中,使用 npm 安装 socket.io-client

npm install socket.io-client

2 在pages下新建两个文件页面

pages/index/index

<template><view class="container" :style="gradientStyle"><view class="header"><text class="title">加入聊天室</text></view><view class="input-container"><input class="nickname-input" type="text" v-model="nickname" @confirm="joinChatroom" placeholder="请输入真实昵称" /><input class="code-input" type="text" v-model="code" @confirm="joinChatroom" placeholder="请输入验证码" /></view><view class="button-container"><button class="join-button" @click="joinChatroom">点击加入</button></view></view>
</template><script>
export default {data() {return {nickname: '', // 用户输入的昵称code: '', // 验证码输入colorIndex: 0, // 用于记录当前颜色索引gradientInterval: null // 定时器引用};},computed: {gradientStyle() {return {background: this.getDynamicGradient()};}},methods: {getDynamicGradient() {const colors = ['#ff7e5f','#feb47b','#ff6a6a','#ffba6a','#fffb6a','#6aff6a','#6afffb','#6a6aff','#ba6aff','#ff6aff'];// 计算背景颜色return `linear-gradient(135deg, ${colors[this.colorIndex]}, ${colors[(this.colorIndex + 1) % colors.length]})`;},joinChatroom() {if (this.nickname.trim() === '') {uni.showToast({title: '你必须给老子输入你的昵称',icon: 'error'});return;}if (this.code.trim() !== '1210') {uni.showToast({title: '验证码错误!请输入',icon: 'error'});return;}// 将用户的昵称保存到全局或跳转到聊天页面uni.navigateTo({url: `/pages/list/list?nickname=${this.nickname}&password=${this.code}`});}},created() {// 创建定时器以更新背景颜色this.gradientInterval = setInterval(() => {this.colorIndex = (this.colorIndex + 1) % 10; // 每秒改变颜色索引this.$forceUpdate(); // 强制更新以应用新的背景色}, 1000);},beforeDestroy() {clearInterval(this.gradientInterval); // 清除定时器}
}
</script><style scoped>
.container {display: flex;flex-direction: column;align-items: center;justify-content: center;height: 100vh;transition: background 1s ease; /* 背景渐变的过渡效果 */
}.header {margin-bottom: 20px;
}.title {font-size: 36rpx;font-weight: bold;color: #fff; /* 修改标题颜色为白色 */
}.input-container {margin-bottom: 20px;width: 80%;
}.nickname-input, .code-input {width: 100%;height: 80rpx;padding: 0 20rpx;border: 1px solid #ccc;border-radius: 10rpx;font-size: 32rpx;background-color: #fff;margin-bottom: 10px; /* 为验证码输入框增加底部间距 */
}.button-container {width: 80%;
}.join-button {width: 100%;height: 80rpx;background-color: #007aff;color: #fff;font-size: 32rpx;border: none;border-radius: 10rpx;text-align: center;line-height: 80rpx;
}.join-button:active {background-color: #005bb5;
}
</style>

 pages/list/list:

<template><view class="container" :style="gradientStyle"><view class="header"><text class="user-count">当前用户数量: {{ userCount }}</text></view><view class="message-container"><view v-for="(message, index) in messages" :key="index":class="{'my-message': message.data.name === name, 'other-message': message.data.name !== name}"><text>{{ message.data.name }}: {{ message.data.text }}</text><text style="margin-left: 100px;font-weight: normal;font-size: 12px;">{{message.time}}</text></view></view><view class="input-container"><input v-model="text" placeholder="输入您的消息" class="input" @confirm="sendMessage"/><button @click="sendMessage" class="send-button">发送</button></view></view>
</template><script>
import io from 'socket.io-client';export default {data() {return {name: '',text: '',code:'',userCount: 0,messages: [],socket: null,colorIndex: 0, // 用于记录当前颜色索引gradientInterval: null // 定时器引用};},onLoad(e) {this.name = e.nickname; // 从传递的参数中获取昵称this.code=e.password},onShow() {if (this.code !== '1210') {// 如果不符合条件,返回上一页uni.navigateTo({url: '/pages/index/index'})}},computed: {gradientStyle() {return {background: this.getDynamicGradient()};}},methods: {getDynamicGradient() {const colors = ['#ff7e5f','#feb47b','#ff6a6a','#ffba6a','#fffb6a','#6aff6a','#6afffb','#6a6aff','#ba6aff','#ff6aff'];// 计算背景颜色return `linear-gradient(135deg, ${colors[this.colorIndex]}, ${colors[(this.colorIndex + 1) % colors.length]})`;},sendMessage() {if (this.validateInput()) {const message = {name: this.name,text: this.text,};this.socket.emit('msgToServer', message);this.text = '';}},receivedUsers(message) {this.userCount = message;},receivedMessage(message) {this.messages.push(message);},validateInput() {return this.name.length > 0 && this.text.length > 0;},disconnectSocket() {if (this.socket) {this.socket.disconnect(); // 断开 WebSocket 连接this.socket = null; // 清空 socket 对象}},},created() {this.socket = io('http://192.168.31.76:3000/chat');this.socket.on('msgToClient', (message) => {this.receivedMessage(message);});this.socket.on('users', (message) => {this.receivedUsers(message);});// 创建定时器this.gradientInterval = setInterval(() => {this.colorIndex = (this.colorIndex + 1) % 10; // 每秒改变颜色索引this.$forceUpdate(); // 强制更新以应用新的背景色}, 1000);},beforeDestroy() {this.disconnectSocket(); // 在组件销毁前断开 WebSocket 连接clearInterval(this.gradientInterval); // 清除定时器},
};
</script><style scoped>
.container {display: flex;flex-direction: column;height: 100vh;
}.header {display: flex;justify-content: flex-end;padding: 10px;
}.user-count {margin-right: 20px;color: #fff;font-weight: 700;
}.message-container {flex: 1;overflow-y: auto;padding: 10px;
}.my-message {text-align: right;background-color: #f0f0f0;margin: 10px 0;padding: 10px;border-radius: 5px;width: fit-content;max-width: 80%;align-self: flex-end;margin-left: auto;font-weight: 700;
}.other-message {text-align: left;background-color: orange;margin: 5px 0;padding: 10px;border-radius: 5px;width: fit-content;max-width: 80%;align-self: flex-start;margin-right: auto;color: #fff;font-weight: 700;
}.input-container {display: flex;position: fixed;bottom: 0;width: 100%;padding: 10px;background-color: rgba(255, 255, 255, 0.9); /* 半透明背景 */
}.input {flex: .98;margin-right: 5px;background-color: #f0f0f0;padding: 10px;border-radius: 5px;
}.send-button {width: 100px;background-color: #007aff;color: white;border: none;border-radius: 5px;height: 45px;
}
</style>

3 然后运行uni项目

下面给大家提供代码地址,可以与你的同事们私密聊天

github:GitHub - dashen-lvweijie/chatRoom: nest.js的简单聊天室功能

 


http://www.ppmy.cn/server/123228.html

相关文章

深入探索机器学习中的目标分类算法

在当今数据驱动的世界中&#xff0c;机器学习&#xff08;Machine Learning, ML&#xff09;正逐渐成为解决问题的重要工具。在众多机器学习任务中&#xff0c;目标分类&#xff08;Classification&#xff09;算法尤其受到关注。本文将深入探讨目标分类算法的基本概念、常见类…

深度学习(4):torch.nn.Module

文章目录 一、是什么二、nn.Module 的核心功能三、nn.Module 的基本用法1. 定义自定义模型2. 初始化模型3. 模型的使用 四、nn.Module 的关键特性1. 自动注册子模块和参数2. forward 方法3. 不需要定义反向传播 五、常用的内置模块六、示例&#xff1a;创建一个简单的神经网络1…

打造双模兼容npm包:无缝支持require与import

为了实现一个npm包同时支持require和import&#xff0c;你需要确保你的包同时提供了CommonJS和ES6模块的入口点。这通常是通过在package.json文件中指定main和module字段来实现的&#xff0c;以及在构建过程中生成两种不同模块格式的文件。 以下是具体步骤&#xff1a; 设置pa…

使用C#,MSSQL开发的钢结构加工系统

很久以前的项目&#xff0c;上位机使用C#开发。数据库使用mssql。控制系统选用了三菱PLC&#xff0c;上位机和PLC之间走ModbusTCP通讯协议。 主要功能&#xff1a;读取加工文件&#xff08;csv格式&#xff09;&#xff0c;导入到数据库&#xff0c;并根据机床刀具规则&#x…

【计算机网络 - 基础问题】每日 3 题(二十七)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏&…

QTday1代码的形式实现登录框

代码注释 main.cpp #include "widget.h"#include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);//调用应用程序类的有参构造的实例化对象Widget w;//调用自定义的有参构造实例化的对象w.show();//调用该类的父类里的成员函数…

JS 防止网页被嵌套:保障登录页面安全性

网页被恶意嵌套是一种常见的安全威胁&#xff0c;尤其是在处理用户登录页面时。这种嵌套行为可能导致跨站请求伪造&#xff08;CSRF&#xff09;攻击&#xff0c;用户凭证泄露&#xff0c;或是用户体验的严重损害。因此&#xff0c;采取有效的JavaScript&#xff08;JS&#xf…

正点原子——DS100示波器操作手册

目录 基础按键&#xff1a; 快捷键 主界面&#xff1a; 垂直设置&#xff1a; 通道设置&#xff1a; 探头比列&#xff1a; 垂直档位&#xff1a; 垂直偏移&#xff1a; 幅度单位&#xff1a; 水平设置&#xff1a; 触发方式&#xff1a; 测量和运算: 光标测量&am…