试读部分
紧急因疫情隔离在家,闲来无事,突发奇想,开发一通讯平台,实时群聊。 思路:客户端发送消息,服务器响应,并将其反馈给在线用户,最后使用内网穿透链接至公网。 技术栈: 前端:原生js + socket 后台服务:nodejs + socket.io + NetAPP 首先,开发过程中需要前端页面作为客户端,然后以nodejs搭建后台服务,安装node环节之前文档有分享,不熟悉者自行查看。在此,首先搭建后台服务: 一、后台服务 1、新建空文件夹作为项目承载,初始化项目,终端代码: yarn init -y 2、新建项目入口文件index.js,内容如下: //index.js
const http = require("http");
const server = http.createServer((req, res)=>{
res.end('后台反馈数据')
})
server.listen(3000) 至此,一个基本服务搭建完成,端口3000 3、启动服务,终端键入如下命令: node index.js 通过localhost:3000即可访问 二、客户端 1、新建文件index.html //index.html
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8 /'>
<title>聊天室</title>
</head>
<body>
<h1>欢迎来到聊天室</h1>
<input type="text" placeholder="请留言!" value="" id="txt">
<button onclick="submit()" id="btn">发送</button>
<ul id="list"></ul>
</body>
<script>
function submit(){
console.……
本文为 VIP 文章,已展示部分内容,开通 VIP 后查看全文
觉得这篇文章有帮助?
点个赞,或赞赏支持作者持续创作