首先下载websocket模块,命令行输入

npm install ws

1.node.js中ws模块创建服务端

// 加载node上websocket模块 ws;
var ws = require("ws"); // 启动基于websocket的服务器,监听我们的客户端接入进来。
var server = new ws.Server({
host: "127.0.0.1",
port: 6080,
}); // 监听接入进来的客户端事件
function websocket_add_listener(client_sock) {
// close事件
client_sock.on("close", function() {
console.log("client close");
}); // error事件
client_sock.on("error", function(err) {
console.log("client error", err);
});
// end // message 事件, data已经是根据websocket协议解码开来的原始数据;
// websocket底层有数据包的封包协议,所以,绝对不会出现粘包的情况。
// 每解一个数据包,就会触发一个message事件;
// 不会出现粘包的情况,send一次,就会把send的数据独立封包。
// 如果我们是直接基于TCP,我们要自己实现类似于websocket封包协议就可以完全达到一样的效果;
client_sock.on("message", function(data) {
console.log(data);
client_sock.send("Thank you!");
});
// end
} // connection 事件, 有客户端接入进来;
function on_server_client_comming (client_sock) {
console.log("client comming");
websocket_add_listener(client_sock);
} server.on("connection", on_server_client_comming); // error事件,表示的我们监听错误;
function on_server_listen_error(err) { }
server.on("error", on_server_listen_error); // headers事件, 回给客户端的字符。
function on_server_headers(data) {
// console.log(data);
}
server.on("headers", on_server_headers);

2.node.js中ws模块创建客户端

var ws = require("ws");

// url ws://127.0.0.1:6080
// 创建了一个客户端的socket,然后让这个客户端去连接服务器的socket
var sock = new ws("ws://127.0.0.1:6080");
sock.on("open", function () {
console.log("connect success !!!!");
sock.send("HelloWorld1");
sock.send("HelloWorld2");
sock.send("HelloWorld3");
sock.send("HelloWorld4");
sock.send(Buffer.alloc(10));
}); sock.on("error", function(err) {
console.log("error: ", err);
}); sock.on("close", function() {
console.log("close");
}); sock.on("message", function(data) {
console.log(data);
});

3.网页客户端创建(使用WebApi --->WebSocket)

<!DOCTYPE html>
<html>
<head>
<title>skynet websocket example</title>
</head>
<body>
<script>
var ws = new WebSocket("ws://127.0.0.1:6080/index.html"); ws.onopen = function(){
alert("open");
ws.send("WebSocket hellowrold!!");
};
ws.onmessage = function(ev){
alert(ev.data);
};
ws.onclose = function(ev){
alert("close");
};
ws.onerror = function(ev){
console.log(ev);
alert("error");
};
</script>
</body>
</html>

node.js中ws模块创建服务端和客户端,网页WebSocket客户端的更多相关文章

  1. node.js中express模块创建服务器和http模块客户端发请求

    首先下载express模块,命令行输入 npm install express 1.node.js中express模块创建服务端 在js代码同文件位置新建一个文件夹(www_root),里面存放网页文 ...

  2. node.js中net网络模块TCP服务端与客户端的使用

    node.js中net模块为我们提供了TCP服务器和客户端通信的各种接口. 一.创建服务器并监听端口 const net = require('net'); //创建一个tcp服务 //参数一表示创建 ...

  3. node.js中net模块创建服务器和客户端(TCP)

    node.js中net模块创建服务器和客户端 1.node.js中net模块创建服务器(net.createServer) // 将net模块 引入进来 var net = require(" ...

  4. 网络聊天室---node.js中net网络模块TCP服务端与客户端的使用

    //1.简单创建 net服务器 // const net = require("net"); // const server = net.createServer((c)=> ...

  5. node.js中module模块的理解

    node.js中使用CommonJS规范实现模块功能,一个单独的文件就是一个单独的模块.通过require方法实现模块间的依赖管理. 通过require加载模块,是同步操作. 加载流程如下: 1.找到 ...

  6. [转]【NODE】用WS模块创建加密的WS服务(WSS)

    [From] https://luojia.me/2015/07/21/%E3%80%90node%E3%80%91%E7%94%A8ws%E6%A8%A1%E5%9D%97%E5%88%9B%E5% ...

  7. 在 Node.js 中引入模块:你所需要知道的一切都在这里

    本文作者:Jacob Beltran 编译:胡子大哈 翻译原文:http://huziketang.com/blog/posts/detail?postId=58eaf471a58c240ae35bb ...

  8. node.js的http模块创建基本Web服务器

    首先下载node.js模块.终端执行命令 npm i node -g 引入http核心模块 const http =require('http') 引入文件系统模块 const fs =require ...

  9. Web 前端模块出现的原因,以及 Node.js 中的模块

    模块出现原因 简单概述 随着 Web 2.0 时代的到来,JavaScript 不再是以前的小脚本程序了,它在前端担任了更多的职责,也逐渐地被广泛运用在了更加复杂的应用开发的级别上. 但是 JavaS ...

随机推荐

  1. AndroidsStudio_找Bug

    新版本不再提供Android Monitor,但在Logcat中可以找到运行日志,但在Regex中要勾选Show only... 另外设置一个控件记得加id.

  2. RHEL6.2 ORACLE11G

    今天翻文件看到这个系统的安装截图才发现自己没写,补上. 惯例还是用到的所有参数均位于文末附录 启动虚拟机进入系统安装 选择跳过硬盘检测 选择语言 选择基本存储设备 设定主机名 设定口令 选择使用所有空 ...

  3. LeetCode 973 K Closest Points to Origin 解题报告

    题目要求 We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, ...

  4. 启动虚拟机提示"Units specified don’t exist SHSUCDX can’t install"

    新建虚拟机快速分区后启动报"Units specified don’t exist SHSUCDX can’t install",试过网上说的 修改BIOS设置方法不起作用 修改虚 ...

  5. 洛谷P4562 [JXOI2018]游戏 数论

    正解:数论 解题报告: 传送门! 首先考虑怎么样的数可能出现在t(i)那个位置上?显然是[l,r]中所有无法被表示出来的数(就约数不在[l,r]内的数嘛QwQ 所以可以先把这些数筛出来 具体怎么筛的话 ...

  6. c# string 扩展方法

    场景:只显示一字符串的前50个字符,多余的用“...”省略号替代 如果不用扩展方法当然也可以实现,写一个静态方法,如下: public class StringUtil { /// <summa ...

  7. opencv图片拼接报错cv::Stitcher::ERR_NEED_MORE_IMGS (1)

    #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "o ...

  8. 接口测试工具-Jmeter使用笔记(三:管理请求服务器信息和Headers参数)

    如果使用Jmeter同时执行多个http请求任务,就需要创建多个HTTP取样器,每一个取样器都来手动填写服务器信息和端口号,会非常消耗时间. 解决方法:Jmeter之HTTP请求默认值 1.添加方式 ...

  9. python datetime 模块

    import datetime datetime.datetime.now() 打印本地当前时间 >>> print(datetime.datetime.now()) 2017-12 ...

  10. MySQL的nnodb引擎表数据分区存储

    Symlinks are fully supported only for MyISAM tables. 对应Innodb引擎数据文件放到其他目录 mysql> SHOW VARIABLES L ...