1

We are using SocketRocket implementing a chat feature. But the problem is when init the SocketRocket, it filtered by the server side login filter, so it can't do http three times handshake success. When we remove the server side login filter, the SocketRocket can success talk to server-side, but server-side can't know who is the user.

So I'm thinking whether it is the reason the SocketRocket don't sync cookie with AFNetworking, cause our login is using AFNetworking library. So is there anybody know how to sync the cookies between them or if you think it is other reason, please let me know. Thanks in advance.

Here is the method we used to connect to server using SocketRocket:

-(void)connectWebSocket{
    _webSocket.delegate = nil;
    _webSocket = nil;
    NSString *urlString = ChatUrl;
    SRWebSocket *newWebSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:urlString]];
    newWebSocket.delegate = self;
    [newWebSocket open];
}
Sudheer Kolasani
  • 283
  • 5
  • 28
astarring
  • 154
  • 1
  • 5

1 Answers1

0

I just had the same problem. You need to add your cookies as request cookies on the websocket. If your websocket URL is different than your login URL (eg. wss://www.example.com vs https://www.example.com), you'll need to copy the cookies from the base URL. For your example above:

-(void)connectWebSocket{
    _webSocket.delegate = nil;
    _webSocket = nil;
    NSString *urlString = ChatUrl;
    SRWebSocket *newWebSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:urlString]];

    // Set the cookies. Need to use the base URL that your login credentials are on.
    NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:BaseUrl];
    newWebSocket.requestCookies = cookies;

    newWebSocket.delegate = self;
    [newWebSocket open];
}
mofojed
  • 1,342
  • 9
  • 11