当前位置: 首页 > news >正文

lws-minimal-ws-server前端分析

index.html

index.html是前端入口

<html>
 <head>
  <meta charset=utf-8 http-equiv="Content-Language" content="en"/>
  <!-- 引入js -->
  <script src="/example.js"></script>
 </head>
	<body>
		<img src="libwebsockets.org-logo.svg">
		<img src="strict-csp.svg"><br>
	
		LWS chat <b>minimal ws server example</b>.<br>
		Chat is sent to all browsers open on this page.
		<br>
		<br>
		<!-- 文本框 -->
		<textarea id=r readonly cols=40 rows=10></textarea><br>
		<!-- 输入框 -->
		<input type="text" id=m cols=40 rows=1>
		<!-- 发送按键 -->
		<button id=b>Send</button>
	</body>
</html>

example.js

example.js 为index.html提供了处理的逻辑


function get_appropriate_ws_url(extra_url)
{
	var pcol;
	// 获得页面上的url
	var u = document.URL;

	/*
	 * We open the websocket encrypted if this page came on an
	 * https:// url itself, otherwise unencrypted
	 */

	// 去掉http://或者https://
	if (u.substring(0, 5) === "https") {
		pcol = "wss://";
		u = u.substr(8);
	} else {
		pcol = "ws://";
		if (u.substring(0, 4) === "http")
			u = u.substr(7);
	}

	// 去掉/后面的
	u = u.split("/");

	/* + "/xxx" bit is for IE10 workaround */

	//回来的url就城了ws://地址或者wss://地址
	return pcol + u[0] + "/" + extra_url;
}

//创建ws服务
function new_ws(urlpath, protocol)
{
	return new WebSocket(urlpath, protocol);
}

//加载监听
document.addEventListener("DOMContentLoaded", function() {
	
	//创建ws服务
	var ws = new_ws(get_appropriate_ws_url(""), "lws-minimal");
	try {
		//ws启动时
		ws.onopen = function() {
			//不禁用输入框和按键
			document.getElementById("m").disabled = 0;
			document.getElementById("b").disabled = 0;
		};
	
		//ws接收到数据包时
		ws.onmessage =function got_packet(msg) {
			//把收到的内容写到文本框加回车
			document.getElementById("r").value =
				document.getElementById("r").value + msg.data + "\n";
			//调整scrollTop
			document.getElementById("r").scrollTop =
				document.getElementById("r").scrollHeight;
		};
	
		//ws连接关闭时
		ws.onclose = function(){
			// 输入框和发送按键禁用
			document.getElementById("m").disabled = 1;
			document.getElementById("b").disabled = 1;
		};
	} catch(exception) {
		alert("<p>Error " + exception);  
	}
	
	//按键点击发送
	function sendmsg()
	{
		//发送内容
		ws.send(document.getElementById("m").value);
		//清空内容
		document.getElementById("m").value = "";
	}
	
	//为b按键增加一个click监听
	document.getElementById("b").addEventListener("click", sendmsg);
	
}, false);


http://www.dtcms.com/a/69868.html

相关文章:

  • YOLO11 使用入门
  • Qt常用控件之Layout总篇
  • Python(学习一)
  • Mac 上编译 Ragflow
  • Manus 技术探索 - 使用 gVisor 在沙箱内运行 Ubuntu 容器并通过远程浏览器访问
  • 【A2DP】深入解读A2DP中通用访问配置文件(GAP)的互操作性要求
  • python速通小笔记
  • 关于单一职责原则
  • 星型组网模块的两种交互方式优缺点解析
  • 【国际研讨会】2025年3-5月通信、算法、电气工程、自动化等领域国际学术会议征稿开启!大型学术盛宴!
  • console.log(MyEnum[0])和console.log(MyEnum.A)区别
  • Vue 自定义指令深度解析与应用实践
  • Mac下安装Zed以及Zed对MCP(模型上下文协议)的支持
  • 【eNSP实战】配置DHCP中继为非直连网段分配IP地址
  • AMI BIOS适配声卡
  • 数字孪生像魔镜,映照出无限可能的未来
  • leetcode0030 串联所有单词的子串-hard
  • WPF Prism事件聚合器EventAggregator
  • SQL与NoSQL的区别
  • Langchian构建代理
  • Django系列教程(12)——连接MySQL和PostgreSQL数据库
  • Devops CI/CD
  • 洛谷 P1962:斐波那契数列 ← 矩阵快速幂
  • JavaScript介绍-变量、常量、数据类型、类型转换
  • 车载以太网测试-13【网络层-IGMP协议】
  • 计算机网络——NAT
  • 深入解析大语言模型的 Function Call 实现—— 以 Qwen2.5为例
  • C# NX二次开发:在多个体的模型中如何实现拉伸操作布尔减
  • Java入职篇(5)—— IDEA快捷键
  • 使用 Docker 部署前端项目全攻略