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

failed to get response from server #45

Closed
XDinEuro opened this issue Dec 20, 2022 · 5 comments
Closed

failed to get response from server #45

XDinEuro opened this issue Dec 20, 2022 · 5 comments

Comments

@XDinEuro
Copy link

XDinEuro commented Dec 20, 2022

Hi, I am using sendRequest() API, and always failed to get response even though I send it from server with same request id.

Here is the detailed code.

const ws = new WebSocketAsPromised('ws://127.0.0.1:8000/ws', {
          packMessage: data => JSON.stringify(data),
          unpackMessage: data => JSON.parse(data),
         attachRequestId: (data, requestId) => Object.assign({id: requestId}, data), // attach requestId to message as `id` field
         extractRequestId: data => data && data.id, 
      });
ws.onMessage.addListener(message => console.log("received ", message));
ws.open();

// send request 
await ws.sendRequest({ task: 'move_ptp' }, { requestId: 1 })
.then((response) => { console.log("responsed"); console.log(response) });

console.log("finished");

I have a server running behind, sending response with same message after 1 second receiving request.

When I run the code I only see the log in onMessage, but not in promise, and I didn't see "finished".

I wonder, did I configure something wrong ? Or I didn't extract requestID from received message correctly ?

@XDinEuro
Copy link
Author

@vitalets

@XDinEuro
Copy link
Author

By the way, if I use
ws.sendPacked({ task: 'move_ptp' });
const response = await this.ws?.waitUnpackedMessage(data => data && data.task === 'move_ptp')
console.log("response is ", response)

it works fine.

@vitalets
Copy link
Owner

Hi @XDinEuro
Could you show logs from client and server side to be sure that all required messages sent?

@XDinEuro
Copy link
Author

XDinEuro commented Jan 3, 2023

Hi, thanks for the reply.

I just changed the code to

// send request 
await ws.open().then(() => {ws.sendRequest({ task: 'move_ptp' }, { requestId: 1 })})
.then((response) => { console.log("responsed"); console.log(response) });

Then it works.

I think the reason is I shouldn't mix synchronous call and asynchronous call together.

@vitalets
Copy link
Owner

Yeap!
You could also replace all then with await:

// send request 
await ws.open();
const response = await ws.sendRequest({ task: 'move_ptp' }, { requestId: 1 });
console.log("responsed"); 
console.log(response);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants