我用libwebsocket在C++中实现了websockets服务器。当我使用或不使用TLS从任何web浏览器打电话时,它都能工作。当我使用TooTallNate/Java库从android应用程序调用它时,如果服务器不使用数字证书,它就能工作,但如果服务器使用数字证书(如果我想使用TLS就需要它),服务器将被阻塞(并且不会响应新消息),应用程序在onClose(int代码、字符串原因、布尔远程)上得到一个1006错误。不管是否发生,我都会在客户端使用SSL。这个问题出现在libwebsockets.c的第2040行:
eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);它会阻塞服务器,直到我杀死android应用程序。
这是应用程序的相关代码:
class WebSocketChatClient extends WebSocketClient {
public WebSocketChatClient( URI serverUri ) {
super( serverUri );
}
public WebSocketChatClient( URI serverUri , Draft protocolDraft , Map<String,String> httpHeaders )
{
super(serverUri,protocolDraft,httpHeaders);
}
@Override
public void onOpen( ServerHandshake handshakedata ) {
System.out.println( "Connected" );
send("prueba");
}
@Override
public void onMessage( String message ) {
System.out.println( "got: " + message );
}
@Override
public void onClose( int code, String reason, boolean remote ) {
System.out.println( "Disconnected" );
//System.exit( 0 );
}
@Override
public void onError( Exception ex ) {
ex.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
URI uri;
try
{
uri=new URI("wss://192.168.1.128:8080");
Map<String,String> headers=new HashMap<String,String>();
headers.put("Sec-WebSocket-Protocol", "dumb-increment-protocol");
headers.put("User-Agent", "My Android App");
wsClient = new WebSocketChatClient(uri,new Draft_17(),headers);
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
wsClient.setWebSocketFactory( new DefaultSSLWebSocketClientFactory( sslContext ) );
wsClient.connect();
}
catch(URISyntaxException e)
{
e.printStackTrace();
}
}这是服务器上的相关代码:
int port = 8080;
const char *intrface = NULL;
struct libwebsocket_context *context;
// we're not using ssl
const char *cert_path = "c:\\mydir\\1921681128.crt";
const char *key_path = "c:\\mydir\\1921681128.key";
// no special options
int opts = 0;
// create libwebsocket context representing this server
context = libwebsocket_create_context(port, intrface, protocols,
libwebsocket_internal_extensions,
cert_path, key_path, NULL,-1, -1, opts);
if (context == NULL) {
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
printf("starting server...\n");
// infinite loop, to end this server send SIGTERM. (CTRL+C)
int k=0;
while (1) {
int res=libwebsocket_service(context, 50);
// libwebsocket_service will process all waiting events with their
// callback functions and then wait 50 ms.
// (this is a single threaded webserver and this will keep our server
// from generating load while there are not requests to process)
}我想知道我这一方是否有任何bug,或者它是否是libwebsocket、openssl或库上的一个bug。如果我使用或不使用TLS调用此服务器:ws:// this .websocket.org,客户机就可以工作。即使java客户机上有错误,我也想知道ssl_read是否正确的行为,即使客户端已经检测到问题并返回错误,也会被阻塞。(对于我来说,openssl代码上的ssl_read有点模糊,我无法理解它)
你好,Juanjo
更新: libwebsockets.c代码在这里:https://github.com/davidgaleano/libwebsockets/blob/master/lib/libwebsockets.c,看起来我有一个以前的版本。现在线路是2037年。考虑到没有任何版本,而且它似乎是从原来的温暖猫/libwebsockets的旧版本中分离出来的,我想主要的问题就在这个库中。
发布于 2015-02-05 15:33:30
解决方案是使用原始的libwebsocket库(而不是叉),并实现TrustManager包装器以传递给sslContext.init。此包装器独立验证服务器证书。
https://stackoverflow.com/questions/28170631
复制相似问题