payload: {"action":"created","issue":{"url":"https://api.github.com/repos/websockets/ws/issues/1608","repository_url":"https://api.github.com/repos/websockets/ws","labels_url":"https://api.github.com/repos/websockets/ws/issues/1608/labels{/name}","comments_url":"https://api.github.com/repos/websockets/ws/issues/1608/comments","events_url":"https://api.github.com/repos/websockets/ws/issues/1608/events","html_url":"https://github.com/websockets/ws/issues/1608","id":473280359,"node_id":"MDU6SXNzdWU0NzMyODAzNTk=","number":1608,"title":"Help wanted: Object Oriented Server and Client","user":{"login":"sneels","id":52665751,"node_id":"MDQ6VXNlcjUyNjY1NzUx","avatar_url":"https://avatars1.githubusercontent.com/u/52665751?v=4","gravatar_id":"","url":"https://api.github.com/users/sneels","html_url":"https://github.com/sneels","followers_url":"https://api.github.com/users/sneels/followers","following_url":"https://api.github.com/users/sneels/following{/other_user}","gists_url":"https://api.github.com/users/sneels/gists{/gist_id}","starred_url":"https://api.github.com/users/sneels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sneels/subscriptions","organizations_url":"https://api.github.com/users/sneels/orgs","repos_url":"https://api.github.com/users/sneels/repos","events_url":"https://api.github.com/users/sneels/events{/privacy}","received_events_url":"https://api.github.com/users/sneels/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2019-07-26T10:03:06Z","updated_at":"2019-07-26T12:47:41Z","closed_at":null,"author_association":"NONE","body":"So, as the title suggests, this is not really an issue with things not working, but rather a request for help in getting my project to be more Object Oriented.\r\n\r\nRight now my server class looks terrible with code like:\r\n\r\n```js\r\nwss.on('connection', function connection(ws, request) {\r\n// Some stuff my code does when a client connects\r\n    ws.on('message', function (message) {\r\n        // some stuff my code does when a client sends a message to the server\r\n    }\r\n```\r\n\r\nI'd rather have it be that my Server class has code like this:\r\n```js\r\nclass Server {\r\n   constructor() {\r\n     //constructor code\r\n   }\r\n   \r\n   Start() {\r\n      // Start WebSocket Server\r\n   }\r\n\r\n   // Event Handlers\r\n   OnConnection(params)\r\n\r\n   OnError(params)\r\n\r\n   OnHeaders(params)\r\n\r\n   OnListening(params)\r\n\r\n   OnEvent(params)\r\n}\r\n```\r\n_(params are the parameters/arguments _ that need to be passed along with the event (eg. ws and request for OnConnection)_\r\n\r\nI'd like to have a similar thing going for the Client (both the ws inside of Server as well as the code where a client connects to a server).\r\n\r\nI have tried to do things along the lines of \r\n```js\r\nwss.on('connection', this.OnConnection(ws, request));\r\nOnConnection(ws, request) {\r\n// Stuff happens here\r\n}\r\n```\r\nhowever, things stop working properly or at all in those sub levels, especially if I attach ws to a different class called ServerClient.\r\n\r\nI am unsure how to properly implement your library as wel as keep my methods and functions clean and as close to OOP as possible.\r\nDo I need to extend your code? And if so, how do I make sure my extended class still works (eg the client, I do not want the client to make a connection on creation since I let it access an authentication page first to get a token used in the verifyClient option when creating the WebSocket.Server\r\n\r\nThanks in advance for any advice and/or tips\r\n\r\nps: my current code works just fine, I am just dissatisfied with how the code is set up. \r\n\r\nEdit: corrected the markdown"},"comment":{"url":"https://api.github.com/repos/websockets/ws/issues/comments/515437077","html_url":"https://github.com/websockets/ws/issues/1608#issuecomment-515437077","issue_url":"https://api.github.com/repos/websockets/ws/issues/1608","id":515437077,"node_id":"MDEyOklzc3VlQ29tbWVudDUxNTQzNzA3Nw==","user":{"login":"sneels","id":52665751,"node_id":"MDQ6VXNlcjUyNjY1NzUx","avatar_url":"https://avatars1.githubusercontent.com/u/52665751?v=4","gravatar_id":"","url":"https://api.github.com/users/sneels","html_url":"https://github.com/sneels","followers_url":"https://api.github.com/users/sneels/followers","following_url":"https://api.github.com/users/sneels/following{/other_user}","gists_url":"https://api.github.com/users/sneels/gists{/gist_id}","starred_url":"https://api.github.com/users/sneels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sneels/subscriptions","organizations_url":"https://api.github.com/users/sneels/orgs","repos_url":"https://api.github.com/users/sneels/repos","events_url":"https://api.github.com/users/sneels/events{/privacy}","received_events_url":"https://api.github.com/users/sneels/received_events","type":"User","site_admin":false},"created_at":"2019-07-26T12:47:41Z","updated_at":"2019-07-26T12:47:41Z","author_association":"NONE","body":"> > So, as the title suggests, this is not really an issue with things not working, but rather a request for help in getting my project to be more Object Oriented.\r\n> \r\n> If it's not an issue [then](https://github.com/websockets/ws/issues/1575#issuecomment-497999842)\r\n\r\nIt's still somewhat an issue, since you are able to attach a function to the events, one would assume you can use an existing function within your class/scope do handle it for you, which it does on the first level, but as soon as you try to attach the ws (which also has events that can be handled by methods inside your class) to a class called Client and attach it there, the whole thing breaks and it is unable to do anything, it's not recognizing itself, requests come through as undefined, etc...\r\n\r\nThe only reason I say it's not really an issue is that as long as you don't try and split off the functionality and do everything in nested functions\r\n```js\r\nStart() {\r\n   wss.on('connection', function connection(ws, request) {\r\n      ws.on('message', function (message) {\r\n\r\n      });\r\n  });\r\n}\r\n```\r\nThe above works perfectly fine\r\n\r\n```js\r\n   Start() {\r\n     wss.on('connection', this.OnConnection);\r\n   }\r\n\r\n   OnConnection(ws, request) {\r\n        var client = new Client();\r\n        ws.on('message', client.OnMessage);\r\n   } \r\n```\r\nclient.js\r\n```js\r\nclass Client {\r\n    constructor() {\r\n        this.ws;\r\n    }\r\n\r\n    OnMessage(message) {\r\n        console.log(message);\r\n    }\r\n\r\n    Send(message) {\r\n        this.ws.send(message);\r\n    }\r\n}\r\n```\r\n\r\nThe above does not work, Client does not work as intended and is unable to recogniwe ws within Send (at least it was for me) and would not be able recognize message in OnMessage either"}}
other: {"actor":{"display_login":"sneels"}}