{"_id":"@cgfamily/think-websocket-ws","name":"@cgfamily/think-websocket-ws","dist-tags":{"latest":"1.0.4"},"versions":{"1.0.4":{"name":"@cgfamily/think-websocket-ws","description":"ws adapter for websocket","version":"1.0.4","author":{"name":"lushijie","email":"lushijie1218@126.com"},"scripts":{"lint":"eslint index.js"},"contributors":[{"name":"lushijie","email":"lushijie1218@126.com"}],"main":"index.js","dependencies":{"debug":"^2.6.1","think-helper":"^1.1.3","think-mock-http":"^1.0.6","ws":"^3.3.1"},"devDependencies":{"ava":"^0.18.2","babel-core":"^6.22.1","babel-eslint":"^7.1.1","coveralls":"^2.12.0","eslint":"~4.2.0","eslint-config-think":"~1.0.1","mock-require":"^2.0.1","mockery":"~2.1.0","nyc":"^7.0.0"},"keywords":[],"repository":{"type":"git","url":"git+https://github.com/thinkjs/think-websocket-ws.git"},"engines":{"node":">=6.0.0"},"bugs":{"url":"https://github.com/thinkjs/think-websocket-ws/issue"},"gitHead":"6d12f515434974f2129c3f7dcf83558822a037d7","homepage":"https://github.com/thinkjs/think-websocket-ws#readme","_id":"@cgfamily/think-websocket-ws@1.0.4","_nodeVersion":"14.18.2","_npmVersion":"6.14.15","dist":{"integrity":"sha512-O1SOwBCe/iGxHpAddWSoB2pOihc6AXan/Ng53z0t+yS9kU2YGvvdwzKFVadbD9ThBVhh8He0PvYN/fVcEQF43w==","shasum":"d17f86db5e14f53138fa3b1816f8bb838129888e","tarball":"https://registry.npmjs.org/@cgfamily/think-websocket-ws/-/think-websocket-ws-1.0.4.tgz","fileCount":11,"unpackedSize":14245,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCxJ8Qs3AyKN6J/WMIP2GjZ1/tSxzH4m3yU6Z3kZOAgSAIgR6m4ascaRlDhWtvCdnCGelZhJzeSNIi/EBaCtWoEkEg="}]},"_npmUser":{"name":"cgfamily","email":"18751290129@163.com"},"directories":{},"maintainers":[{"name":"cgfamily","email":"18751290129@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/think-websocket-ws_1.0.4_1705302879519_0.5954945348786547"},"_hasShrinkwrap":false}},"time":{"created":"2024-01-15T07:14:39.394Z","1.0.4":"2024-01-15T07:14:39.678Z","modified":"2024-01-15T07:14:39.950Z"},"maintainers":[{"name":"cgfamily","email":"18751290129@163.com"}],"description":"ws adapter for websocket","homepage":"https://github.com/thinkjs/think-websocket-ws#readme","keywords":[],"repository":{"type":"git","url":"git+https://github.com/thinkjs/think-websocket-ws.git"},"contributors":[{"name":"lushijie","email":"lushijie1218@126.com"}],"author":{"name":"lushijie","email":"lushijie1218@126.com"},"bugs":{"url":"https://github.com/thinkjs/think-websocket-ws/issue"},"readme":"# think-websocket-ws\r\n[![npm](https://img.shields.io/npm/v/think-websocket-ws.svg?style=flat-square)](https://www.npmjs.com/package/think-websocket-ws)\r\n\r\nThinkJS 3.X's ws adapter for websocket.\r\n\r\n## Install\r\n\r\n```\r\nnpm install think-websocket-ws --save\r\n```\r\n\r\n## How to Config\r\n\r\nEdit config file `src/config/adapter.js`, add options:\r\n\r\n```js\r\nconst ws = require('think-websocket-ws');\r\nexports.websocket = {\r\n  type: 'ws',\r\n  common: {\r\n    // common config\r\n  },\r\n  ws: {\r\n    handle: ws,\r\n    path: '/ws',\r\n    messages: [{\r\n      close: '/ws/close',\r\n      open: '/ws/open',\r\n      addUser: '/ws/addUser'\r\n    }]\r\n  }\r\n}\r\n```\r\n\r\nMore options see at [ws doc](https://github.com/websockets/ws/blob/master/doc/ws.md).\r\n\r\nEdit config file `src/config/config.js`, add options:\r\n\r\n```js\r\nmodule.exports = {\r\n  // ...\r\n  stickyCluster: true\r\n  // ...\r\n}\r\n```\r\n\r\n## Work with Action\r\n\r\n```js\r\n// server\r\nmodule.exports = class extends think.Controller {\r\n  constructor(...arg) {\r\n    super(...arg);\r\n  }\r\n  openAction() {\r\n    console.log('ws open');\r\n    return this.success();\r\n  }\r\n  closeAction() {\r\n    console.log('ws close');\r\n    return this.success();\r\n  }\r\n  addUserAction() {\r\n    console.log('addUserAction ...');\r\n    console.log(this.wsData); // this.req.websocketData, 'thinkjs'\r\n    console.log(this.websocket); // this.req.websocket, websocket instance\r\n    console.log(this.isWebsocket); // this.isMethod('WEBSOCKET'), true\r\n    return this.success();\r\n  }\r\n}\r\n\r\n// client\r\nvar ws = new WebSocket('ws://127.0.0.1:8360/ws');\r\n\r\nws.onopen = function() {\r\n  console.log('open...');\r\n}\r\n\r\nws.onerror = function(evt) {\r\n  console.log(evt);\r\n}\r\n\r\nws.onmessage = function(data) {\r\n  console.log(data);\r\n}\r\n\r\n$('.send').on('click', function(evt) {\r\n  var username = $.trim($('.usernameInput').val());\r\n  var room = $.trim($('.roomInput').val());\r\n  ws.send(JSON.stringify({\r\n    event: 'addUser',\r\n    data: {\r\n      username: username,\r\n      room: room\r\n    }\r\n  }));\r\n});\r\n```\r\n\r\n## emit\r\n\r\nYou can emit event to the current socket in Action through `this.emit`:\r\n\r\n```js\r\n  // server\r\n  module.exports = class extends think.Controller {\r\n    constructor(...arg) {\r\n      super(...arg);\r\n    }\r\n    openAction() {\r\n      this.emit('opend', 'This client opened successfully!');\r\n      return this.success();\r\n    }\r\n  }\r\n\r\n  // client\r\n  ws.onmessage = function(data) {\r\n    data = JSON.parse(data.data);\r\n    console.log(data.event); // opend\r\n    console.log(data.data);  // This client opened successfully!\r\n  }\r\n```\r\n\r\n## broadcast\r\n\r\nYou can broadcast event to all sockets in Action through method `this.broadcast`:\r\n\r\n```js\r\n  // server\r\n  module.exports = class extends think.Controller {\r\n    constructor(...arg) {\r\n      super(...arg);\r\n    }\r\n    openAction() {\r\n      this.broadcast('joined', 'There is a new client joined successfully!');\r\n      return this.success();\r\n    }\r\n  }\r\n\r\n  // client\r\n  ws.onmessage = function(data) {\r\n    data = JSON.parse(data.data);\r\n    console.log(data.event); // joined\r\n    console.log(data.data);  // There is a new client joined successfully!\r\n  }\r\n```\r\n","readmeFilename":"README.md"}