4

我正在构建一个监控用户位置的应用程序,并通过套接字将其发送到我们的服务器。在应用程序启动时,应用程序连接到我们服务器上的套接字,我们是否能够向服务器发送数据。

但是当用户从手机切换到wifi时,连接就会断开。我试图弄清楚如何简单地重新连接(自动),但我不知道如何。有人能帮我吗?

我现在使用的代码(LocationRecorder.m):

//
//  LocationRecorder.m
//

#import "LocationRecorder.h"
#import <Cordova/CDV.h>
#import <coreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>

@implementation LocationRecorder

@synthesize locationManager;

NSInputStream *inputStream;
NSOutputStream *outputStream;

- (void)startUpdates
{
    // this only runs once when the app starts up
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];

    // connect to the socket
    [self initNetworkCommunication];

    // set a timer for every 5 seconds
    [NSTimer scheduledTimerWithTimeInterval:5
                                     target:self
                                   selector:@selector(timerInterval:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)timerInterval:(NSTimer *) timer
{
    CLLocation *location    = [locationManager location];

    // process location data
    [self processLocationData:location];
}

- (void)processLocationData:(CLLocation *)location
{
    // format all the data to prepare it for sending it to the server

    NSString *response  = [NSString stringWithFormat:stringFormat,
                           utctime,
                           utcdate,
                           lat,
                           lng,
                           alt,
                           speed,
                           heading,
                           imei,
                           numsat,
                           battery,
                           cellno];

    //NSLog(@"%@", @"tx");

    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];

    // check for connection
    if ([outputStream streamStatus] != NSStreamStatusOpen) {
        NSLog(@"%@", @"reconnecting...");
        [outputStream open];
    }
    [outputStream write:[data bytes] maxLength:[data length]];
}

- (void)initNetworkCommunication
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"***.***.***.***", 9000, &readStream, &writeStream);

    outputStream = (NSOutputStream *)writeStream;
    // we don't initialize the inputStream since we don't need one (the server does not (yet) talk back)

    [outputStream setDelegate:self];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream open];
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
    switch (streamEvent)
    {
        case NSStreamEventNone:
            // can not connect to host, no event fired
            break;

        case NSStreamEventOpenCompleted:
            // connected!
            NSLog(@"%@", @"connected to socket");
            break;

        case NSStreamEventErrorOccurred:
            // Connection problem
            NSLog(@"%@", @"connection lost");
            [theStream open]; //this does not work :-(
            break;

        case NSStreamEventEndEncountered:
            // connection closed
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [theStream release];
            theStream = nil;
            break;

        default:
            // unknown or untracked event
            break;
    }
}

@end
4

1 回答 1

1

感谢 Martin R 的简单解决方案。

我添加[outputStream close]到 NSStreamEventErrorOccurred,在我将数据发送到 processLocationData 中的套接字之前,我添加了以下代码:

// check for connection
if ([outputStream streamStatus] != NSStreamStatusOpen) {
    NSLog(@"reconnecting...", nil);
    [self initNetworkCommunication];
}
于 2013-05-20T10:23:54.133 回答