{"id":19069571,"url":"https://github.com/srect/websocket","last_synced_at":"2026-05-03T11:33:52.325Z","repository":{"id":114173329,"uuid":"154352621","full_name":"sRect/WebSocket","owner":"sRect","description":"WebSocket聊天室","archived":false,"fork":false,"pushed_at":"2018-11-08T15:44:46.000Z","size":60,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-22T03:42:00.596Z","etag":null,"topics":["chatroom","koa2","socket-io","websocket"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sRect.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-23T15:29:09.000Z","updated_at":"2018-11-08T15:44:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"8419087f-a248-4c6f-b6d0-d95d1c392055","html_url":"https://github.com/sRect/WebSocket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sRect/WebSocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sRect%2FWebSocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sRect%2FWebSocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sRect%2FWebSocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sRect%2FWebSocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sRect","download_url":"https://codeload.github.com/sRect/WebSocket/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sRect%2FWebSocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32567417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T06:36:36.687Z","status":"ssl_error","status_checked_at":"2026-05-03T06:36:09.306Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatroom","koa2","socket-io","websocket"],"created_at":"2024-11-09T01:14:48.982Z","updated_at":"2026-05-03T11:33:52.308Z","avatar_url":"https://github.com/sRect.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## koa2 + websocket\n[WebSocket是时候展现你优秀的一面了](https://juejin.im/post/5bc7f6b96fb9a05d3447eef8)\n\n\u003e 客户端\n```javascript\n// 只需要new一下就可以创建一个websocket的实例\n// 我们要去连接ws协议\n// 这里对应的端口就是服务端设置的端口号9999\nlet ws = new WebSocket('ws://localhost:9999');\n\n// onopen是客户端与服务端建立连接后触发\nws.onopen = () =\u003e {\n  ws.send(\"你好，服务端\");\n}\n\n// onmessage是当服务端给客户端发来消息的时候触发\nws.onmessage = function (res) {\n  console.log(res);   // 打印的是MessageEvent对象\n  // 真正的消息数据是 res.data\n  document.write(`来自服务端的消息：${res.data}`);\n};\n```\n\n\u003e 服务端\n```javascript\nconst Koa = require(\"koa\");\nconst Router = require('koa-router');\nconst satic = require(\"koa-static\"); // 静态服务\nconst fs = require(\"fs\");\nconst path = require(\"path\");\n\nconst app = new Koa();\nconst router = new Router();\n\nrouter.get('/', async (ctx, next) =\u003e { // get请求\n  ctx.response.type = \"html\";\n  // let path = ctx.request.path;\n  ctx.response.body = await render();\n  next();\n})\n\napp.use(router.routes())\n  .use(router.allowedMethods());\n\napp.listen(3000, () =\u003e {\n  console.log('====your app is running at port 3000=====')\n})\n\napp.use(satic(path.join(__dirname)))\n\n/**\n * 开始创建一个websocket服务\n */\nconst Server = require('ws').Server;\n// 这里是设置服务器的端口号，和上面的3000端口不用一致\nconst ws = new Server({ port: 9999 });\n\n// 监听服务端和客户端的连接情况\nws.on('connection', function (socket) {\n  // 监听客户端发来的消息\n  socket.on('message', function (msg) {\n    console.log(`来自客户端的消息：${msg}`);   // 这个就是客户端发来的消息\n    // 来而不往非礼也，服务端也可以发消息给客户端\n    socket.send(`你好，客户端！`);\n  });\n});\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrect%2Fwebsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrect%2Fwebsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrect%2Fwebsocket/lists"}