970507 - 使用websocket时出现问题 [英] 970507 - problem in working with websocket

查看:117
本文介绍了970507 - 使用websocket时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:

In the following code:

#define Uses_Info
#define Uses_WebSocketServer
#include "ws.h"

int on_connect(WebSocketServer *server);
void on_message(const char *buf);
void on_disconnect();

void* CALLBACK ws_stream_on_connect(WebSocketServer *server)
{
	Info("ws_stream_on_connect");
	int n = 5;
	server->send(server, MESSAGE_TYPE_BINARY, (unsigned char *)&n, sizeof(int));
	Info("after test");
	return server;
	//on_connect(server);
	//return NULL;  // return value is not used in client.
}

size_t CALLBACK ws_stream_on_message(void *plugin_private, const WebSocketServer *server, const int type, unsigned char *buf, const size_t buf_size)
{
	//Info("ws_stream_on_message");
	on_message(buf);
	return 0;
}

void CALLBACK ws_stream_on_disconnect(void *plugin_private, const WebSocketServer *server)
{
	Info("ws_stream_on_disconnect");
	on_disconnect();
}

/*
 * Since we are returning a pointer to static memory, there is no need for a
 * "destroy" function.
 */

static WebSocketPlugin s_plugin = { sizeof(WebSocketPlugin),
                WEBSOCKET_PLUGIN_VERSION_0, NULL, /* destroy */
                ws_stream_on_connect, ws_stream_on_message,
                ws_stream_on_disconnect };

extern EXPORT WebSocketPlugin * CALLBACK ws_stream_init()
{
	Info("ws_stream_init");
    return &s_plugin;
}



我没有得到测试后日志。 Info是一个在syslog中记录字符串的函数。我看到了ws_stream_on_connect,但不是这个。

有什么问题?

操作系统是Ubuntu 12.04。



我尝试了什么:



我试着看看apache2 error.log中记录了什么,我也提到了syslog,但是没有记录在那里。


I don't get the "after test" log. Info is a function which logs the strings in syslog. I see the "ws_stream_on_connect", but not this one.
What's the problem?
OS is Ubuntu 12.04.

What I have tried:

I tried to see what's logged in apache2 error.log and I also referred to syslog, but nothing is logged there.

推荐答案

可能抛出异常?你可以包装服务器 - >发送一个try / catch语句并在catch块中记录一条消息。



BR,

Manfred
Maybe an exception was thrown ? You can wrap the server-> send in a try/catch statement and log a message in the catch block.

BR,
Manfred


哦,我发现了什么!在mod_websocket.c中,在函数handle_websocket_connection中,在调用conf-> plugin-> on_connect之前确实是ws_stream_on_connect,在state.mutex上调用apr_thread_mutex_lock。然后在mod_websocket_plugin_send中我们再次看到锁。 BMO,这意味着mod_websocket.c不希望从ws_stream_on_connect中调用send。你觉得怎么样?

但是,当我看到mod_websocket_plugin_send代码时,我看到这个锁之后的一个部分表明可以从主线程中调用send!这是调用mod_websocket_send_internal的地方。怎么可能?!

现在,我可以通过创建一个线程并从中发送数据来绕过这个问题。这将在ws_stream_on_connect返回后完成。
oh i found something! in mod_websocket.c, in function handle_websocket_connection, before calling conf->plugin->on_connect which is indeed ws_stream_on_connect, apr_thread_mutex_lock is called on state.mutex. then in mod_websocket_plugin_send we see the lock again. BMO, this means that mod_websocket.c doesn't expect send be called from within ws_stream_on_connect. what do you think?
but, when i see mod_websocket_plugin_send code, i see a section after this lock which indicates that the send may be called from within the main thread! this is where mod_websocket_send_internal is called. how is may be possible?!
for now, i could bypass the problem by creating a thread and sending data from within it. this will be done after ws_stream_on_connect returns.


这篇关于970507 - 使用websocket时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆