📜 ⬆️ ⬇️

We use web sockets in our iOS application

Good afternoon, dear readers of Habrahabr!

Today I want to tell you how to quickly and easily connect web sockets into your iOS application using the chat of the famous cryptocurrency exchange. We realize this with the help of a convenient open solution SocketRocket .

This approach can be useful for:
')

Interested please under the cat!

Add SocketRocket to the project

On the githaba, several installation options are described, I will describe the one I use myself.

  1. Add to our project all the files from the “SocketRocket” group
  2. Add the following frameworks to the project:
    • libicucore.dylib
    • CFNetwork.framework
    • Security.framework
    • Foundation.framework
  3. Add a Google HTML decryptor to the project (see why later)

Nothing complicated! Everything is ready to use all the power of web sockets.

We initialize sockets

With the methods of information retrieval , deduction and with the help of responsive developers of the exchange, I came across an open source, from which I pulled out the necessary URL for chat sockets:

NSURL *url = [NSURL URLWithString:@"wss://ws.pusherapp.com/app/4e0ebd7a8b66fa3554a4?protocol=6&client=js&version=2.0.0&flash=false"]; 

Create the socket object itself with the necessary request, open it and make ourselves a delegate:

 NSURLRequest *request = [NSURLRequest requestWithURL:url]; SRWebSocket *rusSocket = [[SRWebSocket alloc] initWithURLRequest:request]; rusSocket.delegate = self; [rusSocket open]; 

Full code for the setupSockets method
 - (void)setupSockets { NSURL *url = [NSURL URLWithString:@"wss://ws.pusherapp.com/app/4e0ebd7a8b66fa3554a4?protocol=6&client=js&version=2.0.0&flash=false"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; SRWebSocket *rusSocket = [[SRWebSocket alloc] initWithURLRequest:request]; rusSocket.delegate = self; [rusSocket open]; } 


SRWebSocketDelegate methods

In every self-respecting framework under Objective-C, there must be delegates - and even more so in web-apps. We implement two delegate methods — the first one, which is called after the connection is established and the socket is opened, and the second, when the message is received.

 - (void)webSocketDidOpen:(SRWebSocket *)webSocket { NSString *helloMsg = @"{\"event\":\"pusher:subscribe\",\"data\":{\"channel\":\"chat_ru\"}}"; [webSocket send:helloMsg]; } 

Everything is very simple here: as soon as the sockets open, we subscribe to alerts from the Russian chat. By analogy, you can register and "chat_en".

Next, we describe the method of receiving messages from the website:

 - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { message = [[message stringByReplacingOccurrencesOfString:@"///" withString:@""] stringByReplacingOccurrencesOfString:@"\\\\\\" withString:@""]; message = [message gtm_stringByUnescapingFromHTML]; } 

Again, it's simple. In the first line of the method, we get rid of garbage, in the second we use a category from Google to convert HTML characters to readable by the user. Message is our message - then you can parse it as you please.

Sample message
{"Event": "msg", "data": "\" {"uid": "467754", "login": "BTCalexxx", "msg": "who can use for a short ltc pump to 15?", "Msg_id": 12268748, "date": "03/04/14 07:37:50", "usr_clr": "# 8da0b9"} \ "", "channel": "chat_en"}

Close the connection

Always close the connection when you no longer need it! This can be done by the following method:

 [rusSocket close]; 

Conclusion

Thank you so much for reading to the end! With this uncomplicated method we now have the opportunity to have up-to-date information about chatting with one of the cryptocurrency exchanges.

In the next article I can describe how to create your WhatsApp using open tools for 4-5 hours of work. Of course, if you, dear readers, it will be interesting.

Source: https://habr.com/ru/post/214579/


All Articles