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

how to send customized messages #15

Open
ZJLi2013 opened this issue Mar 17, 2020 · 4 comments
Open

how to send customized messages #15

ZJLi2013 opened this issue Mar 17, 2020 · 4 comments

Comments

@ZJLi2013
Copy link

i modified the test.c:

int onopen(wsclient *c, char* msg) {
	fprintf(stderr, "onopen called: %d\n", c->sockfd);
	libwsclient_send(c,  msg);
	return 0;
}

and modified wsclient.c with :

void libwsclient_onopen(wsclient *client, int (*cb)(wsclient *c, char* ), char* msg) {
	pthread_mutex_lock(&client->lock);
	client->onopen = cb(client, msg);
	pthread_mutex_unlock(&client->lock);
}

after a few changes and rebuilt, I tested it, but the client always give:

Unhandled control frame received.  Opcode: 9

and the server bother:

"lib/python3.7/site-packages/websockets/protocol.py", line 803, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

what's the correct way to send customized message ?

@ZJLi2013
Copy link
Author

I checked the code a little more, onopen() is actually called inside handshake thread, which is not legacy to pass another char* message. but neither there is a global alive status variable to tell the client-server channel is alive, so if I directly call libwsclient_send(), it also has issues.

@PixMod
Copy link

PixMod commented Mar 18, 2020

Try call libwsclient_send() in another thread. onopen() is in handshake thread may not suitable for sending data.

@ZJLi2013
Copy link
Author

Try call libwsclient_send() in another thread. onopen() is in handshake thread may not suitable for sending data.

thanks, but I still think there is lack of a connection_status variable to know the channel is opened

@PixMod
Copy link

PixMod commented Mar 19, 2020

Try call libwsclient_send() in another thread. onopen() is in handshake thread may not suitable for sending data.

thanks, but I still think there is lack of a connection_status variable to know the channel is opened

After you received onclose, the websocket client instance already has been freed in end of wsclient_run_thread() thus you cannot call it's interfaces anymore.
You can keepconnection_status in your own project variable when received onopen/onclose/onerror. Ref https://github.com/morrowind/libwsclient/blob/f576ee470eeed668e7a18cbc664fe415b5fe85d1/test/test.c#L83

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