Skip to content

Commit

Permalink
feat: 加入ws开发依赖及测试等 (#14)
Browse files Browse the repository at this point in the history
* test: test

* sv

* fewngo

* 3223

* chore: 加入commit提交规范

* chore: 优化commit规范

* workflow: 增加ci检测及加入pr模板

* fix: 修复pnpm format引起的ci问题

* fix: 修复pnpm lint引起的ci问题

* fix: 修复pnpm lint引起的ci问题

* feat: 初始化websocket客户端

* fix: 修改eslint规则,优化websocket(#6)

* ci: 去除typescript的ci

* refactor: 重构项目,去除typescript

* feat: 加入ws 开发依赖等(#13)
  • Loading branch information
yuhao423 authored Jun 24, 2024
1 parent e32700f commit 6a28b0c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"description": "",
"main": "index.js",
"scripts": {
"dev":"node src/index.js",
"test": "node test/index.js",
"lint": "eslint \"src/*.{js,ts,cjs,json}\" --fix",
"format": "prettier --write \"src/*.{ts,js,cjs,mjs,json}\"",
"typecheck": "tsc --noEmit",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"use strict";
const a = 3;
const map = new Map();
map.set("s", a);
const YuFlux = require("./websocket");

const yuFlux = new YuFlux("http://localhost:8000");
yuFlux.on("upgrade", (res, socket, head) => {
console.error("res.headers");
});
19 changes: 16 additions & 3 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,33 @@ const initWebSocketClient = (websocket, address, protocols, options) => {
req = websocket._req = request(opts);

if (opts.timeout) {
req("timeout", () => {
req.on("timeout", () => {
//这里不能简单的抛出,一定要断开连接并抛出错误
abortHandshake(websocket, req, "握手超时了");
});
}

//error
req("error", () => {
req.on("error", (err) => {
req = websocket._req = null;

emitErrorAndClose();
});

//todo respose 重定向

req("upgrade", (res, socket, head) => {});
req.on("upgrade", (res, socket, head) => {
websocket.emit("upgrade", res);
});

if (opts.finishRequest) {
opts.finishRequest(req, websocket);
} else {
req.end();
}
};

//todo 完善 emitErrorAndClose 函数
const emitErrorAndClose = (err) => {
console.error(err, "err");
};
5 changes: 5 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const weServer = require('./websocketServer')

weServer.on('connection',()=>{
console.error('websocket-server connected');
})
7 changes: 7 additions & 0 deletions test/websocketServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

const ws = require('ws')

const weServer = new ws.WebSocketServer({port:8000})

module.exports = weServer

0 comments on commit 6a28b0c

Please sign in to comment.