mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 11:31:59 +08:00
1ba17074d1
1. The API of SocketRocket is easy for use, it's all asynchronous API. 2. No need to deal with thread communications, it's wrapped inside SocketRocket. 3. Network status change will not make connection closed, it just seems there isn't any status change. 4. Locking screen will trigger an error while libwebsockets or bsd socket will not. 5. Don't depend on openssl library which is a little big. 6. Don't need to use a local CA root certificate (curl ca file is over 2MB) for wss connection 7. Don't trigger signal pipe while closing wss connection 8. Easy to integrate SocketRocket to current WebSocket implementation
267 lines
6.0 KiB
Plaintext
267 lines
6.0 KiB
Plaintext
//
|
|
// WebSocket-apple.m
|
|
// cocos2d_libs
|
|
//
|
|
// Created by James Chen on 7/3/17.
|
|
//
|
|
//
|
|
|
|
#include "network/WebSocket.h"
|
|
#include "base/CCData.h"
|
|
|
|
#import "SocketRocket/SocketRocket.h"
|
|
|
|
@interface WebSocketImpl : NSObject<SRWebSocketDelegate>
|
|
{
|
|
cocos2d::network::WebSocket* _ccws;
|
|
cocos2d::network::WebSocket::Delegate* _delegate;
|
|
std::string _url;
|
|
std::string _selectedProtocol;
|
|
}
|
|
|
|
@property (nonatomic, retain) SRWebSocket* ws;
|
|
|
|
@end
|
|
|
|
//
|
|
|
|
@implementation WebSocketImpl
|
|
|
|
-(id) initWithURL:(const std::string&) url protocols:(NSArray<NSString *> *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates ws:(cocos2d::network::WebSocket*) ccws delegate:(const cocos2d::network::WebSocket::Delegate&) delegate
|
|
{
|
|
if (self = [super init])
|
|
{
|
|
_ccws = ccws;
|
|
_delegate = const_cast<cocos2d::network::WebSocket::Delegate*>(&delegate);
|
|
_url = url;
|
|
NSURL* nsUrl = [[NSURL alloc] initWithString:[[NSString alloc] initWithUTF8String:_url.c_str()]];
|
|
self.ws = [[SRWebSocket alloc] initWithURL:nsUrl protocols:protocols allowsUntrustedSSLCertificates:allowsUntrustedSSLCertificates];
|
|
self.ws.delegate = self;
|
|
[self.ws open];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
-(void) sendString:(NSString*) message
|
|
{
|
|
[self.ws sendString:message error:nil];
|
|
}
|
|
|
|
-(void) sendData:(NSData*) data
|
|
{
|
|
[self.ws sendData:data error:nil];
|
|
}
|
|
|
|
-(void) close
|
|
{
|
|
[self.ws close];
|
|
}
|
|
|
|
-(void) closeAsync
|
|
{
|
|
[self.ws close];
|
|
}
|
|
|
|
-(cocos2d::network::WebSocket::State) getReadyState
|
|
{
|
|
cocos2d::network::WebSocket::State ret;
|
|
SRReadyState state = self.ws.readyState;
|
|
switch (state) {
|
|
case SR_OPEN:
|
|
ret = cocos2d::network::WebSocket::State::OPEN;
|
|
break;
|
|
case SR_CONNECTING:
|
|
ret = cocos2d::network::WebSocket::State::CONNECTING;
|
|
break;
|
|
case SR_CLOSING:
|
|
ret = cocos2d::network::WebSocket::State::CLOSING;
|
|
break;
|
|
default:
|
|
ret = cocos2d::network::WebSocket::State::CLOSED;
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
-(const std::string&) getUrl
|
|
{
|
|
return _url;
|
|
}
|
|
|
|
-(const std::string&) getProtocol
|
|
{
|
|
return _selectedProtocol;
|
|
}
|
|
|
|
-(cocos2d::network::WebSocket::Delegate*) getDelegate
|
|
{
|
|
return (cocos2d::network::WebSocket::Delegate*)_delegate;
|
|
}
|
|
|
|
// Delegate methods
|
|
|
|
-(void)webSocketDidOpen:(SRWebSocket *)webSocket;
|
|
{
|
|
NSLog(@"Websocket Connected");
|
|
if (webSocket.protocol != nil)
|
|
_selectedProtocol = [webSocket.protocol UTF8String];
|
|
_delegate->onOpen(_ccws);
|
|
}
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
|
|
{
|
|
NSLog(@":( Websocket Failed With Error %@", error);
|
|
_delegate->onError(_ccws, cocos2d::network::WebSocket::ErrorCode::UNKNOWN);
|
|
self.ws = nil;
|
|
}
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(nonnull NSString *)string
|
|
{
|
|
std::string message = [string UTF8String];
|
|
cocos2d::network::WebSocket::Data data;
|
|
data.bytes = (char*)message.c_str();
|
|
data.len = message.length() + 1;
|
|
data.isBinary = false;
|
|
data.issued = 0;
|
|
data.ext = nullptr;
|
|
|
|
_delegate->onMessage(_ccws, data);
|
|
}
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithData:(NSData *)nsData
|
|
{
|
|
cocos2d::network::WebSocket::Data data;
|
|
data.bytes = (char*)nsData.bytes;
|
|
data.len = nsData.length;
|
|
data.isBinary = true;
|
|
data.issued = 0;
|
|
data.ext = nullptr;
|
|
_delegate->onMessage(_ccws, data);
|
|
}
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
|
|
{
|
|
_delegate->onClose(_ccws);
|
|
|
|
self.ws = nil;
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
namespace network {
|
|
|
|
void WebSocket::closeAllConnections()
|
|
{
|
|
|
|
}
|
|
|
|
WebSocket::WebSocket()
|
|
: _impl(nil)
|
|
{
|
|
|
|
}
|
|
|
|
WebSocket::~WebSocket()
|
|
{
|
|
_impl = nil;
|
|
}
|
|
|
|
|
|
bool WebSocket::init(const Delegate& delegate,
|
|
const std::string& url,
|
|
const std::vector<std::string>* protocols/* = nullptr*/,
|
|
const std::string& caFilePath/* = ""*/)
|
|
{
|
|
if (url.empty())
|
|
return false;
|
|
|
|
NSMutableArray* nsProtocols = [[NSMutableArray alloc] init];
|
|
if (protocols != nullptr)
|
|
{
|
|
for (const auto& protocol : *protocols)
|
|
{
|
|
[nsProtocols addObject:[[NSString alloc] initWithUTF8String:protocol.c_str()]];
|
|
}
|
|
}
|
|
_impl = [[WebSocketImpl alloc] initWithURL: url protocols:nsProtocols allowsUntrustedSSLCertificates:NO ws: this delegate:delegate];
|
|
return _impl != nil;
|
|
}
|
|
|
|
|
|
void WebSocket::send(const std::string& message)
|
|
{
|
|
if ([_impl getReadyState] == State::OPEN)
|
|
{
|
|
NSString* str = [[NSString alloc] initWithUTF8String:message.c_str()];
|
|
[_impl sendString:str];
|
|
}
|
|
else
|
|
{
|
|
printf("Couldn't send message since websocket wasn't opened!\n");
|
|
}
|
|
}
|
|
|
|
|
|
void WebSocket::send(const unsigned char* binaryMsg, unsigned int len)
|
|
{
|
|
if ([_impl getReadyState] == State::OPEN)
|
|
{
|
|
NSData* data = [[NSData alloc] initWithBytes:binaryMsg length:(NSUInteger)len];
|
|
[_impl sendData:data];
|
|
}
|
|
else
|
|
{
|
|
printf("Couldn't send message since websocket wasn't opened!\n");
|
|
}
|
|
}
|
|
|
|
|
|
void WebSocket::close()
|
|
{
|
|
if ([_impl getReadyState] == State::CLOSING || [_impl getReadyState] == State::CLOSED)
|
|
{
|
|
printf("WebSocket (%p) was closed, no need to close it again!\n", this);
|
|
return;
|
|
}
|
|
|
|
[_impl close];
|
|
}
|
|
|
|
void WebSocket::closeAsync()
|
|
{
|
|
if ([_impl getReadyState] == State::CLOSING || [_impl getReadyState] == State::CLOSED)
|
|
{
|
|
printf("WebSocket (%p) was closed, no need to close it again!\n", this);
|
|
return;
|
|
}
|
|
|
|
[_impl closeAsync];
|
|
}
|
|
|
|
WebSocket::State WebSocket::getReadyState() const
|
|
{
|
|
return [_impl getReadyState];
|
|
}
|
|
|
|
const std::string& WebSocket::getUrl() const
|
|
{
|
|
return [_impl getUrl];
|
|
}
|
|
|
|
const std::string& WebSocket::getProtocol() const
|
|
{
|
|
return [_impl getProtocol];
|
|
}
|
|
|
|
WebSocket::Delegate* WebSocket::getDelegate() const
|
|
{
|
|
return [_impl getDelegate];
|
|
}
|
|
|
|
} // namespace network {
|
|
|
|
NS_CC_END
|