
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
一个 基于 koa 的 ws 模块,检测在线用户,广播消息等功能,支持 cjs 和 esm
npm i koa-my-ws
const Koa = require("koa")
const Router = require("koa-router")
const createWs = require("koa2-ws")
const app = new Koa()
const router = new Router()
const webSocket = createWs()
// 返回一个中间件
app.use(webSocket.middleware)
app.on("error", err => {})
router.get("/ws/:id", async (ctx, next) => {
const { id } = ctx.params
// 升级协议 返回当前ws对象 uniqueId为自定义唯一id:
let ws = await ctx.upgradeWs({
uniqueId: id
})
if (ws) {
ws.on("message", (data, isBinary) => {
console.log(data.toString())
ws.send("发送消息")
webSocket.broadcast("给所有已连接的用户广播消息")
})
}
next()
})
app.use(router.routes())
app.listen(3000)
const createWs = require("koa2-ws")
const webSocket = createWs({
clearOfflineWs: true, // 是否清理掉线的客户端
heartbeatTime: 3000, // 检测时间
// 传入ws的配置详情 --> https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocketserver
wsOptions: {
clientTracking: false,
maxPayload: 69420
}
})
// 使用中间件
app.use(webSocket.middleware)
const Koa = require("koa")
const Router = require("koa-router")
const createWs = require("koa2-ws")
const app = new Koa()
const router = new Router()
const webSocket1 = createWs({ wsName: "ws1" })
const webSocket2 = createWs({ wsName: "ws2" })
// 返回一个中间件
app.use(webSocket1.middleware)
app.use(webSocket2.middleware)
app.on("error", err => {})
router.get("/ws1/:id", async (ctx, next) => {
const { id } = ctx.params
// 升级协议 返回当前ws对象 uniqueId为自定义唯一id:
let ws = await ctx.upgradeWs({
wsName:"ws1"
uniqueId: id
})
if (ws) {
ws.on("message", (data, isBinary) => {
console.log(data.toString())
ws.send("发送消息")
webSocket1.broadcast("给所有已连接的用户广播消息")
})
}
next()
})
router.get("/ws2/:id", async (ctx, next) => {
const { id } = ctx.params
// 升级协议 返回当前ws对象 uniqueId为自定义唯一id:
let ws = await ctx.upgradeWs({
wsName:"ws2"
uniqueId: id
})
if (ws) {
ws.on("message", (data, isBinary) => {
console.log(data.toString())
ws.send("发送消息")
webSocket2.broadcast("给所有已连接的用户广播消息")
})
}
next()
})
app.use(router.routes())
app.listen(3000)
获取连接的 webSocket 实例 Class: WebSocket
webSocket.getClient(uniqueId) 根据创建连接时的 uniqueId 获取客户端,必须搭配 uniqueId 使用webSocket.getClients() 获取所有客户端webSocket.broadcast(data,options,callback) 给所有用户广播消息,内部调用 ws.send 方法
webSocket.wsServer Class: WebSocketServer
FAQs
一个 基于koa 的 ws 模块,检测在线用户,广播消息等功能,支持 cjs 和 esm
We found that koa2-ws demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.