To run the example project, clone the repo, and run pod install from the Example directory first.
HTTP and Websocket can reuse the same path with no conflict.
To add a websocket handler, use addWebsocketHandlerForPath:withProcessBlock.
Usage:
-(void)SetupServer{
GCDWebsocketServer *server = [[GCDWebsocketServer alloc] init];
[server addWebsocketHandlerForPath:@"/" withProcessBlock:^GCDWebsocketServerHandler * _Nullable(GCDWebsocketServerConnection * _Nonnull conn) {
return [GCDWebsocketServerHandler handlerWithConn:conn];
}];
[server addHandlerForMethod:@"GET" path:@"/" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse * _Nullable(__kindof GCDWebServerRequest * _Nonnull request) {
return [GCDWebServerDataResponse responseWithText:@"ok\n"];
}];
NSDictionary *options = @{
GCDWebServerOption_Port: @(9999),
};
NSError *error;
[server startWithOptions:options error:&error];
if(error){
NSLog(@"start server failed for: %@", error);
}
}GCDWebsocketServer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'GCDWebsocketServer'guyingzhao, 572488191@qq.com
GCDWebsocketServer is available under the MIT license. See the LICENSE file for more info.