The Wayback Machine - https://web.archive.org/web/20201104093235/https://github.com/uNetworking/uWebSockets.js/issues/380
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare and use local variables per connection #380

Closed
lzaiko opened this issue Oct 29, 2020 · 2 comments
Closed

Declare and use local variables per connection #380

lzaiko opened this issue Oct 29, 2020 · 2 comments

Comments

@lzaiko
Copy link

@lzaiko lzaiko commented Oct 29, 2020

Hi Alex,

I’m having an issue to declare a local variable foo (see below) which is only visible inside the socket connection handlers (open, message, close).

cost port = 9000;
const app = uWS.App()
          .ws('/', {
              maxPayloadLength: 1024 * 1024,
              idleTimeout: 15,
              open: (ws, req) => {
                  foo = true;
              },
              message: (ws, message, isBinary) => {
                 if(foo) {
                   // do something with the message
                  }
              }
          }).listen(port, token => {
             console.log(`WebSocket server listening on: ${port}`);
          });
});

The variable foo can’t be declared globally, otherwise it will be visible for all the connections (total mess).

I checked the examples but couldn’t find a solution to my problem.
Help appreciated.

@hst-m
Copy link

@hst-m hst-m commented Oct 29, 2020

You've got an extra set of }) at the end that needs to be removed, and open event does not have req so you need to remove that, and if you want to share something across open and message events you should put it in the ws object

open:ws=>{
   ws.foo=true
},
message:(ws,message,isBinary)=>{
   console.log(ws.foo)
}
@lzaiko
Copy link
Author

@lzaiko lzaiko commented Oct 29, 2020

Thanks a mill.

@lzaiko lzaiko closed this Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.