Socket.io-FLSocketIM-iOS
基于Socket.io iOS即时通讯客户端 iOS IM Client based on Socket.io
iOS 代码地址:https://github.com/fengli12321/Socket.io-FLSocketIM-iOS
服务器端代码实现参照:https://github.com/githuanl/node.js-socket.io-server
安卓端代码实现参照:https://github.com/githuanl/socket.io-android-
实现功能
- 文本发送
- 图片发送(从相册选取,或者拍摄)
- 短视频
- 语音发送
- 视频通话
- 其他一些效果(类似QQ底部tabBar,短视频拍摄等)
- 功能扩展中。。。。。
先看看实际效果
使用技术
一、socket.io
socket.io是该项目实现即时通讯关键所在,非常强大;
Socket.io将Websocket和轮询 (Polling)机制以及其它的实时通信方式封装成了通用的接口,并且在服务端实现了这些实时机制的相应代码。
先上代码
1.创建socket连接,通过单例管理类FLSocketManager实现
- (void)connectWithToken:(NSString *)token success:(void (^)())success fail:(void (^)())fail {
NSURL* url = [[NSURL alloc] initWithString:BaseUrl];
/**
log 是否打印日志
forceNew 这个参数设为NO从后台恢复到前台时总是重连,暂不清楚原因
forcePolling 是否强制使用轮询
reconnectAttempts 重连次数,-1表示一直重连
reconnectWait 重连间隔时间
connectParams 参数
forceWebsockets 是否强制使用websocket, 解释The reason it uses polling first is because some firewalls/proxies block websockets. So polling lets socket.io work behind those.
来源:https://github.com/socketio/socket.io-client-swift/issues/449
*/
SocketIOClient* socket;
if (!self.client) {
socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @NO, @"forceNew" : @YES, @"forcePolling": @NO, @"reconnectAttempts":@(-1), @"reconnectWait" : @4, @"connectParams": @{@"auth_token" : token}, @"forceWebsockets" : @NO}];
}
else {
socket = self.client;
socket.engine.connectParams = @{@"auth_token" : token};
}
// 连接超时时间设置为15秒
[socket connectWithTimeoutAfter:15 withHandler:^{
fail();
}];