Communication between iOS App and Server, App and Html

Keywords: socket Mobile

Today let's talk about App Communications!

1. The traditional communication between App and server is well known. It's just that the third-party framework uses Apple's native request mode alive, socket, and all kinds of strange interactions. (vii) At present, the most famous one is AFNetworking. There are many articles on the internet, so I won't say anything about it.

2. Communication between App and App. If it's a long link, I recommend using Cocoa AsyncSocket. This seems like a Facebook framework, but I've studied that it seems impossible to communicate with h5. Because this protocol is not ws protocol, the client can only receive a request header and fail to respond. But there's more than enough communication between apps. Let's take a general look. There are also documents on this website.

2.1 create

GCDAsyncSocket *serviceScoket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];

2.2 Binding ports and specifying access IP

NSError *error = nil;

    [serviceScoket acceptOnPort:8001 error:&error];

//The following can specify that only local access is allowed

//        [serviceScoket acceptOnInterface:@"localhost" port:8001 error:&error];

if (error == nil)

    {

        NSLog(@"Successful opening");

    }

    else

    {

        NSLog(@"Open failure %@",error);

    }

2.3 Then it's important to connect the client proxy in order to implement the proxy's one-step operation in the proxy. socket You need to save it with an array, or you can link to this socket It's disconnected.

#pragma mark GCDAsyncSocketDelegate

// Connect to client socket

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket

{

    //socket on sock server

    //socket Connected by New socket Client

    NSLog(@"%@----%@",sock, newSocket);

    //Save the client socket of the connection (otherwise the link will automatically disconnect when the new socket is released)

    [self.clientSockets addObject:newSocket];

. . . . . . . . . Other operating networks can communicate as long as two agents are implemented.

}

That's how apps communicate! Is it very simple, haha ha, is an app as a server, a client, of course, can also do communication between local area networks is also possible!

3. Communication between App and Html.

The latest project requires this feature, app as a server, h5 as a display. So we need to set up a server on the App side, not to mention a lot, and introduce the most popular Cocoa HTTP Server framework. 99% of the Internet is about how to transfer files from web pages and mobile phones. Today, let's talk about how Web pages send Post and Get requests to mobile phones.

Note: The Post Request section is the only one I've found for a long time on the Internet that has talked about it. I quoted a section that I forgot who the author was.

3.1 First, when app starts, we create a separate folder for the server to specify the path. It can be created either directly in Documents or directly in projects, because you have to have the absolute path of this folder anyway.

3.2 Setting up Server

self.httpServer = [[HTTPServer alloc] init];

    [_httpServer setType:@"_http.tcp."];

//This sentence refers to the address document that specifies the access path that explains what the api is for.

    //    [_httpServer setInterface:@"localhost"];

    [_httpServer setPort:8000];

//webLocalPath is the folder path you created

[_httpServer setDocumentRoot:webLocalPath];

//Start the server

if([_httpServer start:&error]){

        NSLog(@"Started HTTP Server on port %hu", [_httpServer listeningPort]);

    }

    else{

        NSLog(@"Error starting HTTP Server: %@", error);

    }

So our server is created, but we don't know when h5 is requesting us, and we can't accept parameters. Continue:

CocoaHTTP Server has a class for managing requests and allows us to customize it, which is HTTP Connection.

The HTTP server document says


Interface

Inheriting this class overrides our own request management class

// Extend the request type supported by HTTP Server. GET, HEAD are supported by default, POST is not supported.-

-(BOOL)supportsMethod:(NSString *)method atPath:(NSString *)relativePath{

//parseParams is a self-contained method for extracting transfer parameters. Here we explain receiving requests.

  [[NSNotificationCenter defaultCenter] postNotificationName:ReceiveRequestNotice object:self userInfo:[self parseParams:relativePath]];

 if ([@"POST" isEqualToString:method]) { return YES; } 

 return [super supportsMethod:method atPath:relativePath];}

//response data returned by a measure

- (NSObject *)httpResponseForMethod:(NSString *)method URI:(NSString *)path

{

//We're going to return the string to h5, where we're going to return the corresponding parameters.

    NSString *txt = @"test({wocao:\"1212122112321ss312\"})";

    NSData *data = [txt dataUsingEncoding:NSUTF8StringEncoding];

//Classes that come with HTTPDataResponse

    return [[HTTPDataResponse alloc] initWithData:data];

}


Finally, the server calls it.

[_httpServer setConnectionClass:[XKHTTPConnection class]];

Basically, the main idea is over. If you have any questions, please leave a message.


Statement: All rights reserved, reproduced please indicate the source!

Statement: All rights reserved, reproduced please indicate the source!

Statement: All rights reserved, reproduced please indicate the source!

Posted by likethegoddess on Sun, 19 May 2019 19:15:14 -0700