{"id":18368127,"url":"https://github.com/thinkjs/think-websocket-ws","last_synced_at":"2025-04-06T17:31:37.544Z","repository":{"id":57377015,"uuid":"104728286","full_name":"thinkjs/think-websocket-ws","owner":"thinkjs","description":"websocket for ws","archived":false,"fork":false,"pushed_at":"2021-07-01T15:30:28.000Z","size":8,"stargazers_count":9,"open_issues_count":1,"forks_count":3,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-22T03:51:17.453Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thinkjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-25T09:14:12.000Z","updated_at":"2024-03-24T11:18:04.000Z","dependencies_parsed_at":"2022-09-26T17:00:40.257Z","dependency_job_id":null,"html_url":"https://github.com/thinkjs/think-websocket-ws","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-websocket-ws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-websocket-ws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-websocket-ws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-websocket-ws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thinkjs","download_url":"https://codeload.github.com/thinkjs/think-websocket-ws/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247522316,"owners_count":20952520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-11-05T23:24:43.961Z","updated_at":"2025-04-06T17:31:37.277Z","avatar_url":"https://github.com/thinkjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# think-websocket-ws\n[![npm](https://img.shields.io/npm/v/think-websocket-ws.svg?style=flat-square)](https://www.npmjs.com/package/think-websocket-ws)\n\nThinkJS 3.X's ws adapter for websocket.\n\n## Install\n\n```\nnpm install think-websocket-ws --save\n```\n\n## How to Config\n\nEdit config file `src/config/adapter.js`, add options:\n\n```js\nconst ws = require('think-websocket-ws');\nexports.websocket = {\n  type: 'ws',\n  common: {\n    // common config\n  },\n  ws: {\n    handle: ws,\n    path: '/ws',\n    messages: [{\n      close: '/ws/close',\n      open: '/ws/open',\n      addUser: '/ws/addUser'\n    }]\n  }\n}\n```\n\nMore options see at [ws doc](https://github.com/websockets/ws/blob/master/doc/ws.md).\n\nEdit config file `src/config/config.js`, add options:\n\n```js\nmodule.exports = {\n  // ...\n  stickyCluster: true\n  // ...\n}\n```\n\n## Work with Action\n\n```js\n// server\nmodule.exports = class extends think.Controller {\n  constructor(...arg) {\n    super(...arg);\n  }\n  openAction() {\n    console.log('ws open');\n    return this.success();\n  }\n  closeAction() {\n    console.log('ws close');\n    return this.success();\n  }\n  addUserAction() {\n    console.log('addUserAction ...');\n    console.log(this.wsData); // this.req.websocketData, 'thinkjs'\n    console.log(this.websocket); // this.req.websocket, websocket instance\n    console.log(this.isWebsocket); // this.isMethod('WEBSOCKET'), true\n    return this.success();\n  }\n}\n\n// client\nvar ws = new WebSocket('ws://127.0.0.1:8360/ws');\n\nws.onopen = function() {\n  console.log('open...');\n}\n\nws.onerror = function(evt) {\n  console.log(evt);\n}\n\nws.onmessage = function(data) {\n  console.log(data);\n}\n\n$('.send').on('click', function(evt) {\n  var username = $.trim($('.usernameInput').val());\n  var room = $.trim($('.roomInput').val());\n  ws.send(JSON.stringify({\n    event: 'addUser',\n    data: {\n      username: username,\n      room: room\n    }\n  }));\n});\n```\n\n## emit\n\nYou can emit event to the current socket in Action through `this.emit`:\n\n```js\n  // server\n  module.exports = class extends think.Controller {\n    constructor(...arg) {\n      super(...arg);\n    }\n    openAction() {\n      this.emit('opend', 'This client opened successfully!');\n      return this.success();\n    }\n  }\n\n  // client\n  ws.onmessage = function(data) {\n    data = JSON.parse(data.data);\n    console.log(data.event); // opend\n    console.log(data.data);  // This client opened successfully!\n  }\n```\n\n## broadcast\n\nYou can broadcast event to all sockets in Action through method `this.broadcast`:\n\n```js\n  // server\n  module.exports = class extends think.Controller {\n    constructor(...arg) {\n      super(...arg);\n    }\n    openAction() {\n      this.broadcast('joined', 'There is a new client joined successfully!');\n      return this.success();\n    }\n  }\n\n  // client\n  ws.onmessage = function(data) {\n    data = JSON.parse(data.data);\n    console.log(data.event); // joined\n    console.log(data.data);  // There is a new client joined successfully!\n  }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkjs%2Fthink-websocket-ws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthinkjs%2Fthink-websocket-ws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkjs%2Fthink-websocket-ws/lists"}