基于websocket实现一个简单的网站在线客服聊天室案例

13 篇文章 0 订阅

前言

在网站项目开发中,我们经常会有在线客服的功能,通过网站开启一个聊天室,进行客服解答功能,本节我们使用websocket实现一个简单的网站在线客服聊天功能,效果如下:

正文

  • 后端引入websocket的pom依赖
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

  •  后端初始化注入websocket的ServerEndpointExporter端点
package com.yundi.atp.platform.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
 * @Author: yanp
 * @Description:
 * @Date: 2021/9/15 15:27
 * @Version: 1.0.0
 */
@Configuration
public class WebSocketConfig {
    /**
     * 注入ServerEndpointExporter,
     * 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

  •  后端创建一个websocket服务器,用于前后端通信,发送消息
package com.yundi.atp.platform.websocket;

import com.alibaba.fastjson.JSON;
import com.yundi.atp.platform.module.test.entity.ChatMsg;
import com.yundi.atp.platform.module.test.service.ChatMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author: yanp
 * @Description: WebSocket服务端
 * @Date: 2021/9/15 15:27
 * @Version: 1.0.0
 */
@Slf4j
@Component
@ServerEndpoint("/websocket/chat/{userName}")
public class WebSocketServer {
    private Session session;

    private static CopyOnWriteArraySet<WebSocketServer> webSockets = new CopyOnWriteArraySet<>();

    private static Map<String, Session> sessionPool = new ConcurrentHashMap<>();

    private static ChatMsgService chatMsgService;

    private static String SUPER_ADMIN = "super_admin";

    @Autowired
    public void setWebSocketServer(ChatMsgService chatMsgService) {
        WebSocketServer.chatMsgService = chatMsgService;
    }

    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userName") String userName) {
        List<String> onlineList = new ArrayList<>();
        this.session = session;
        webSockets.add(this);
        sessionPool.put(userName, session);
        log.info("【websocket消息】有新的连接,总数为:" + webSockets.size());
        Iterator<Map.Entry<String, Session>> iterator = sessionPool.entrySet().iterator();
        while (iterator.hasNext()) {
            onlineList.add(iterator.next().getKey());
        }
        onlineList.remove(SUPER_ADMIN);
        Map<String, Object> map = new HashMap<>(16);
        map.put("key", 1);
        map.put("onlineList", onlineList);
        map.put("userList", chatMsgService.getUserList());
        this.sendOneMessage(SUPER_ADMIN, JSON.toJSONString(map));
    }

    @OnClose
    public void onClose(@PathParam(value = "userName") String userName) {
        webSockets.remove(this);
        sessionPool.remove(userName);
        log.info("【websocket消息】连接断开,总数为:" + webSockets.size());
        List<String> onlineList = new ArrayList<>();
        Iterator<Map.Entry<String, Session>> iterator = sessionPool.entrySet().iterator();
        while (iterator.hasNext()) {
            onlineList.add(iterator.next().getKey());
        }
        onlineList.remove(SUPER_ADMIN);
        Map<String, Object> map = new HashMap<>(16);
        map.put("key", 2);
        map.put("onlineList", onlineList);
        map.put("userList", chatMsgService.getUserList());
        this.sendOneMessage(SUPER_ADMIN, JSON.toJSONString(map));
    }

    @OnMessage
    public void onMessage(String message) {
        ChatMsg chatMsg = JSON.parseObject(message, ChatMsg.class);
        chatMsgService.save(chatMsg);
        Map<String, Object> map = new HashMap<>(16);
        map.put("key", 3);
        map.put("data", chatMsg);
        this.sendOneMessage(chatMsg.getSender(), JSON.toJSONString(map));
        this.sendOneMessage(chatMsg.getReceiver(), JSON.toJSONString(map));
    }

    /**
     * 广播消息
     */
    public void sendAllMessage(String message) {
        for (WebSocketServer webSocket : webSockets) {
            log.info("【websocket消息】广播消息:" + message);
            try {
                webSocket.session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 单点消息
     *
     * @param userName
     * @param message
     */
    public void sendOneMessage(String userName, String message) {
        log.info("【websocket消息】单点消息:" + message);
        Session session = sessionPool.get(userName);
        if (session != null) {
            try {
                session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

  • 后端聊天室功能控制层接口
package com.yundi.atp.platform.module.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yundi.atp.platform.common.Result;
import com.yundi.atp.platform.module.test.entity.ChatMsg;
import com.yundi.atp.platform.module.test.service.ChatMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yanp
 * @since 2021-09-17
 */
@Api(tags = "聊天室接口")
@RestController
@RequestMapping("/test/chatMsg")
public class ChatMsgController {
    @Autowired
    private ChatMsgService chatMsgService;

    @ApiOperation(value = "获取聊天室地址")
    @GetMapping(value = "/getWebSocketAddress/{username}")
    public Result getWebSocketAddress(HttpServletRequest request, @PathVariable(value = "username") String username) throws UnknownHostException {
        String address = "ws://" + InetAddress.getLocalHost().getHostAddress() + ":" + request.getServerPort() + request.getContextPath() + "/websocket/chat/" + username;
        return Result.success(address);
    }

    @ApiOperation(value = "获取历史聊天记录")
    @GetMapping(value = "/getHistoryChat/{username}")
    public Result getWebSocketAddress(@PathVariable(value = "username") String username) {
        List<ChatMsg> list = chatMsgService.list(new QueryWrapper<ChatMsg>()
                .and(wrapper -> wrapper.eq("sender", username).or().eq("receiver", username))
                .orderByDesc("create_time"));
        List<ChatMsg> collect = list.stream().sorted(Comparator.comparing(ChatMsg::getCreateTime)).collect(Collectors.toList());
        return Result.success(collect);
    }

    @ApiOperation(value = "获取用户列表")
    @GetMapping(value = "/getUserList")
    public Result getUserList() {
        List<String> userList = chatMsgService.getUserList();
        return Result.success(userList);
    }

}

  •  后端聊天室功能业务层
package com.yundi.atp.platform.module.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yundi.atp.platform.module.test.entity.ChatMsg;
import com.yundi.atp.platform.module.test.mapper.ChatMsgMapper;
import com.yundi.atp.platform.module.test.service.ChatMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yanp
 * @since 2021-09-17
 */
@Service
public class ChatMsgServiceImpl extends ServiceImpl<ChatMsgMapper, ChatMsg> implements ChatMsgService {
    @Autowired
    private ChatMsgMapper chatMsgMapper;

    @Override
    public List<String> getUserList() {
        List<String> userList = chatMsgMapper.getUserList();
        return userList;
    }
}

  •   后端聊天室功能持久层
package com.yundi.atp.platform.module.test.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yundi.atp.platform.module.test.entity.ChatMsg;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yanp
 * @since 2021-09-17
 */
public interface ChatMsgMapper extends BaseMapper<ChatMsg> {
    /**
     * 用户列表
     * @return
     */
    List<String> getUserList();

}

  •  后端聊天室功能持久层Mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yundi.atp.platform.module.test.mapper.ChatMsgMapper">

    <select id="getUserList" resultType="java.lang.String">
        SELECT sender FROM `chat_msg` where sender != 'super_admin' GROUP BY sender
    </select>
</mapper>

  •   前端聊天室功能页面
<template>
  <div class="container">
    <el-card class="box-card">
      <div slot="header">
        <el-row type="flex">
          <el-col :span="1" style="margin: 15px 10px;">
            <img alt="ATP客服" src="@/assets/logo.png" style="width:40px;height:40px;"/>
          </el-col>
          <el-col :span="3" style="line-height: 74px;margin-left: 10px;">
            <span style="display: inline-block;color: white;">ATP客服</span>
          </el-col>
          <el-col :span="20" v-if="username==='super_admin'">
            <h5 style="color: #83ccd2;padding: 0;text-align: right;margin: 50px 20px 0 0;">当前在线人数:{{ online }}</h5>
          </el-col>
          <el-col :span="20" v-else>
            <h5 style="color: #83ccd2;padding: 0 0 2px 0;text-align: right;margin: 50px 20px 0 0;font-size: 18px;">
              {{ username }}</h5>
          </el-col>
        </el-row>
      </div>
      <div class="content" ref="content">
        <el-row type="flex">
          <el-col :span="6" style="background: #eee;min-height: 600px;" v-if="username==='super_admin'">
            <el-tabs v-model="activeName" @tab-click="handleClick" style="position: fixed;width: 190px;margin: 0 2px;">
              <el-tab-pane label="在线用户" name="online">
                <div v-for="item in friend" :key="item" @click="switchUser(item)" :class="item===active?'mark':''">
                  <el-badge :is-dot=msgNotify.includes(item) class="item" type="success">
                    <li style="list-style-type:none;padding: 5px 8px;cursor: pointer;"
                        class="active">
                      {{ item }}
                    </li>
                  </el-badge>
                  <el-divider></el-divider>
                </div>
              </el-tab-pane>
              <el-tab-pane label="全部用户" name="all">
                <div v-for="item in userList" :key="item" @click="switchUser(item)" :class="item===active?'mark':''">
                  <el-badge :is-dot=msgNotify.includes(item) class="item" type="success">
                    <li style="list-style-type:none;padding: 5px 8px;cursor: pointer;"
                        :class="friend.includes(item)?'active':''">
                      {{ item }}
                    </li>
                  </el-badge>
                  <el-divider></el-divider>
                </div>
              </el-tab-pane>
            </el-tabs>
          </el-col>
          <el-col :span="18" v-if="username==='super_admin'">
            <div v-for="item in chatMsgList">
              <el-row type="flex" style="margin-bottom: 20px;" v-if="username===item.sender">
                <el-col :span="2">
                  <img alt="ATP客服" src="@/assets/logo.png" style="width:30px;height:30px;margin: 10px 0px 0px 20px;"/>
                </el-col>
                <el-col :span="22">
                  <el-row type="flex" style="margin-top: 10px;margin-left: 5px;opacity: 0.2;">
                    <el-col :span="7"><span style="padding-left: 20px;">{{ item.sender }}</span></el-col>
                    <el-col :span="7"><span>{{ item.createTime | dataFormat('yyyy-MM-dd HH:mm') }}</span></el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="14" style="margin-left: 8px;margin-top: 5px;">
                      <el-card style="padding: 8px 5px;">
                        {{ item.msg }}
                      </el-card>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <el-row type="flex" style="margin-bottom: 20px;" v-else justify="end">
                <el-col :span="22">
                  <el-row type="flex" style="margin-top: 10px;margin-right: 5px;opacity: 0.2;" justify="end">
                    <el-col :span="6"><span>{{ item.sender }}</span></el-col>
                    <el-col :span="7"><span>{{ item.createTime | dataFormat('yyyy-MM-dd HH:mm') }}</span></el-col>
                  </el-row>
                  <el-row type="flex" justify="end" style="margin-right: 8px;margin-top: 5px;">
                    <el-col :span="14" style="margin-right: 8px;">
                      <el-card style="padding: 8px 5px;">
                        {{ item.msg }}
                      </el-card>
                    </el-col>
                  </el-row>
                </el-col>
                <el-col :span="2">
                  <el-avatar shape="square" size="medium" style="float: right;margin: 10px 20px 0px 0px;">客户</el-avatar>
                </el-col>
              </el-row>
            </div>
          </el-col>
          <el-col :span="24" v-else>
            <div v-for="item in chatMsgList">
              <el-row type="flex" style="margin-bottom: 20px;" v-if="username===item.sender">
                <el-col :span="2">
                  <el-avatar shape="square" size="medium" style="float: right;margin: 10px 20px 0px 0px;">客户</el-avatar>
                </el-col>
                <el-col :span="22">
                  <el-row type="flex" style="margin-top: 10px;opacity: 0.2;margin-left: 20px;">
                    <el-col :span="7"><span style="padding-left: 5px;">{{ item.sender }}</span></el-col>
                    <el-col :span="7"><span>{{ item.createTime | dataFormat('yyyy-MM-dd HH:mm') }}</span></el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="14">
                      <el-card style="padding: 8px 5px;">
                        {{ item.msg }}
                      </el-card>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <el-row type="flex" style="margin-bottom: 20px;" v-else justify="end">
                <el-col :span="22">
                  <el-row type="flex" style="margin-top: 10px;margin-right: 5px;opacity: 0.2;" justify="end">
                    <el-col :span="6"><span>{{ item.sender }}</span></el-col>
                    <el-col :span="7"><span>{{ item.createTime | dataFormat('yyyy-MM-dd HH:mm') }}</span></el-col>
                  </el-row>
                  <el-row type="flex" justify="end" style="margin-top: 5px;">
                    <el-col :span="14">
                      <el-card style="padding: 8px 5px;">
                        {{ item.msg }}
                      </el-card>
                    </el-col>
                  </el-row>
                </el-col>
                <el-col :span="2">
                  <img alt="ATP客服" src="@/assets/logo.png" style="width:30px;height:30px;margin: 10px 0px 0px 20px;"/>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
      </div>
      <div class="operate" v-if="username==='super_admin'">
        <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 3}"
            placeholder="您好!这里是ATP客服部,我是客服小美,很高兴为您服务!"
            v-model="msg">
        </el-input>
        <el-button type="success" size="mini" style="float: right;margin-top: 5px;" @click="sendMsg"
                   :disabled="!(msg && active)">
          发送
        </el-button>
      </div>
      <div class="operate" v-else>
        <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 3}"
            placeholder="您好!这里是ATP客服部,我是客服小美,很高兴为您服务!"
            v-model="msg">
        </el-input>
        <el-button type="success" size="mini" style="float: right;margin-top: 5px;" @click="sendMsg" :disabled="!msg">
          发送
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "ClientChat",
  data() {
    return {
      msg: '',
      chatMsgList: [],
      username: sessionStorage.getItem("username"),
      friend: [],
      online: 0,
      active: '',
      receiver: 'super_admin',
      userList: [],
      activeName: 'online',
      msgNotify:[],
    }
  },
  created() {
    this.getWebSocketAddress();
  },
  methods: {
    //tab切换
    handleClick(tab, event) {
      const _this = this;
      if (tab.name === 'online') {
        if (!_this.active) {
          if (_this.online > 0) {
            _this.active = _this.friend[0];
            _this.activeName = 'online';
            _this.receiver = _this.active;
            _this.getHistoryChat(_this.receiver);
          } else {
            if (_this.userList.length > 0) {
              _this.active = _this.userList[0];
              _this.activeName = 'all';
              _this.receiver = _this.active;
              _this.getHistoryChat(_this.receiver);
            }
          }
        }
      }
      if (tab.name === 'all') {
        if (!_this.active) {
          if (_this.online > 0) {
            _this.active = _this.friend[0];
            _this.activeName = 'online';
            _this.receiver = _this.active;
            _this.getHistoryChat(_this.receiver);
          } else {
            if (_this.userList.length > 0) {
              _this.active = _this.userList[0];
              _this.activeName = 'all';
              _this.receiver = _this.active;
              _this.getHistoryChat(_this.receiver);
            }
          }
        }
      }
    },
    //切换用户
    switchUser(data) {
      if (this.active === data) {
        return;
      }
      this.active = data;
      this.receiver = data;
      //获取历史聊天记录
      this.getHistoryChat(this.receiver);
      this.msgNotify = this.msgNotify.filter(item => item != this.active);
    },
    //获取历史聊天记录
    getHistoryChat(data) {
      this.$http.get('/test/chatMsg/getHistoryChat/' + data).then(res => {
        if (res.data.code === 1) {
          this.chatMsgList = res.data.data;
          this.flushScroll();
        } else {
          this.$message.warning(res.data.msg);
        }
      }).catch(error => {
        this.$message.error(error);
      });
    },
    //获取websocket地址
    getWebSocketAddress() {
      this.$http.get('/test/chatMsg/getWebSocketAddress/' + this.username).then(res => {
        if (res.data.code === 1) {
          if ('WebSocket' in window) {
            this.websocket = new WebSocket(res.data.data);
            this.initWebSocket();
            if (this.username != 'super_admin') {
              this.getHistoryChat(this.username);
            }
          } else {
            this.$message.warning('当前浏览器不支持websocket创建!');
          }
        } else {
          this.$message.warning(res.data.msg);
        }
      }).catch(error => {
        this.$message.error(error);
      });
    },
    //初始化websocket
    initWebSocket() {
      const _this = this;
      _this.websocket.onerror = function (event) {
        _this.$message.error('服务端连接错误!');
      }
      _this.websocket.onopen = function (event) {
        _this.$message.success("连接成功!");
      }
      _this.websocket.onmessage = function (event) {
        let res = JSON.parse(event.data);
        if (res.key === 1) {
          _this.userList = res.userList;
          _this.friend = res.onlineList;
          _this.online = _this.friend.length;
          if (!_this.active) {
            if (_this.online > 0) {
              _this.active = _this.friend[0];
              _this.activeName = 'online';
              _this.receiver = _this.active;
              _this.getHistoryChat(_this.receiver);
            } else {
              if (_this.userList.length > 0) {
                _this.active = _this.userList[0];
                _this.activeName = 'all';
                _this.receiver = _this.active;
                _this.getHistoryChat(_this.receiver);
              }
            }
          }
        }
        if (res.key === 2) {
          _this.userList = res.userList;
          _this.friend = res.onlineList;
          _this.online = _this.friend.length;
          if (!_this.active) {
            if (_this.online > 0) {
              _this.active = _this.friend[0];
              _this.activeName = 'online';
              _this.receiver = _this.active;
              _this.getHistoryChat(_this.receiver);
            } else {
              if (_this.userList.length > 0) {
                _this.active = _this.userList[0];
                _this.activeName = 'all';
                _this.receiver = _this.active;
                _this.getHistoryChat(_this.receiver);
              }
            }
          }
        }
        if (res.key === 3) {
          if (_this.username === res.data.sender) {
            _this.chatMsgList.push(res.data);
            _this.flushScroll();
          } else {
            if (res.data.sender === 'super_admin') {
              _this.chatMsgList.push(res.data);
              _this.flushScroll();
            } else {
              if (res.data.sender === _this.active) {
                _this.chatMsgList.push(res.data);
                _this.flushScroll();
              } else {
                //发送其它用户处理
                _this.msgNotify.push(res.data.sender);
              }
            }
          }
        }
      }
      _this.websocket.onclose = function (event) {
        _this.$message.warning('服务端已关闭!');
      }
    },
    //发送消息
    sendMsg() {
      if (this.msg.trim().length === 0) {
        this.$message.warning('不能发送空消息!');
        return;
      }
      let chatMsg = {};
      chatMsg.msg = this.msg;
      chatMsg.sender = this.username;
      chatMsg.createTime = new Date();
      chatMsg.receiver = this.receiver;
      this.websocket.send(JSON.stringify(chatMsg));
      this.msg = '';
      this.flushScroll();
    },
    //刷新滚动条
    flushScroll() {
      let content = this.$refs.content;
      setTimeout(() => {
        content.scrollTop = content.scrollHeight;
      }, 100);
    },
  }
}
</script>

<style scoped lang="scss">
.container {
  padding-top: 50px;

  .box-card {
    margin: auto;
    width: 800px;
    height: 800px;
    max-height: 900px;

    ::v-deep .el-card__header {
      background: #867ba9 !important;
      border-bottom: none;
      padding: 0;
    }

    ::v-deep .el-card__body {
      padding: 0px !important;
      position: relative;

      .content {
        height: 600px;
        background: #ddd;
        overflow-y: auto;

        .el-divider--horizontal {
          margin: 0;
        }

        .active {
          color: #0080ff;
        }

        .mark {
          background: #deb068;
        }

        .item {
          margin-top: 10px;
          margin-right: 10px;
        }
      }

      .operate {
        padding: 5px 15px;
      }
    }
  }
}
</style>

  •  聊天室页面效果

结语

本节内容到这里就结束了,我们下期见。。。

  • 4
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论
是的,一个 WebSocket 可以被多个页面使用。下面是一个简单的 Node.js 实现 WebSocket 聊天的示例代码: ```javascript const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function connection(ws) { console.log('A new client connected'); ws.on('message', function incoming(message) { console.log('Received message:', message); // Broadcast the message to all clients wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { client.send(message); } }); }); ws.on('close', function close() { console.log('Client disconnected'); }); }); ``` 这个聊天会监听端口 8080,并且当有新的客户端连接时,会打印一条消息。当客户端发送消息时,服务器会将消息广播给所有连接的客户端。当客户端关闭连接时,也会打印一条消息。 在客户端,你可以使用 WebSocket API 来连接到服务器并发送和接收消息。下面是一个简单的 HTML 页面,它使用 JavaScript 来实现客户端逻辑: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>WebSocket Chat</title> </head> <body> <h1>WebSocket Chat</h1> <input type="text" id="message" /> <button onclick="sendMessage()">Send</button> <ul id="messages"></ul> <script> const socket = new WebSocket('ws://localhost:8080'); socket.addEventListener('message', function(event) { const messages = document.getElementById('messages'); const li = document.createElement('li'); li.innerText = event.data; messages.appendChild(li); }); function sendMessage() { const message = document.getElementById('message').value; socket.send(message); } </script> </body> </html> ``` 这个页面包含一个文本框和一个按钮,用于发送消息。它还包含一个列表,用于显示所有收到的消息。当收到来自服务器的消息时,它会将消息添加到列表中。当用户点击按钮时,它会将文本框中的内容发送到服务器。在这个示例中,我们假设服务器运行在本地主机上,端口号为 8080。如果你将这个代码保存为 chat.html 并在本地主机上运行,你应该能够在多个浏览器标签中打开它,并在它们之间发送消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厉害哥哥吖

您的支持是我创作下去的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值