WebSocketManager.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // WebSocketManager.h
  3. // Anchor
  4. //
  5. // Created by 翟玉磊 on 2019/3/26.
  6. // Copyright © 2019 翟玉磊. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "SRWebSocket.h"
  10. #define WebSocketManager_Shared [WebSocketManager sharedSocketManager]
  11. NS_ASSUME_NONNULL_BEGIN
  12. @protocol WebSocketManagerDelegate <NSObject>
  13. - (void)webSocketManagerDidReceiveMessageWithObject:(id)object;
  14. @end
  15. /**
  16. 连接回调 errCode:0 说明连接成功
  17. */
  18. typedef void (^IMConnectCompletionHandler)(int errCode, NSString *errMsg);
  19. /**
  20. 离开回调 errCode:0 说明离开成功
  21. */
  22. typedef void (^IMCloseCompletionHandler)(int errCode, NSString *errMsg);
  23. @interface WebSocketManager : NSObject
  24. @property (nonatomic, strong) SRWebSocket *webSocket;
  25. @property (nonatomic, readwrite, weak) id<WebSocketManagerDelegate> delegate;
  26. + (instancetype)sharedSocketManager;//单例
  27. /// 建立长连接
  28. - (void)connectServerWithUrl:(NSString *)url connectCompletionHandler:(IMConnectCompletionHandler)completionHandler;
  29. /// 关闭长连接
  30. - (void)SRWebSocketCloseCompletionHandler:(IMCloseCompletionHandler)completionHandler;
  31. /// 发送数据给服务器
  32. - (void)sendDataToServer:(id)data;
  33. //重新连接服务器
  34. - (void)reConnectServer;
  35. @end
  36. NS_ASSUME_NONNULL_END