nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

Objective-C compiling #74

Closed danipralea closed 9 years ago

danipralea commented 9 years ago

Hello! Is there any way we can have a guide to building the library to an existing Objective-C project?

I have tried with both importing the files manually, and Cocoapods, and I have no success in doing so. Thanks in advance!

nuclearace commented 9 years ago

This will work if you manually include the files

@import Foundation;
#import "SwiftIOObjc-Swift.h" // Your auto-generated Objective-C interface header

@interface Tester : NSObject

@property (readwrite, nonatomic) SocketIOClient* socket;

- (id)init;
- (void)connect;
- (void)sendMessage:(NSString*)msg;

@end

@implementation Tester : NSObject

- (id)init
{
    if (self = [super init]) {
        self.socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" opts:@{@"log": @NO, @"connectParams": @{@"thing": @"value"}}];

        [[self socket] onAny:^(SocketAnyEvent* event) {
            // NSLog(@"Got event: %@, with items: %@", event.event, event.items);
        }];

        [[self socket] onObjectiveC:@"connect" callback:^(NSArray* data, void (^ack)(NSArray*)) {
            NSLog(@"socket connected");
        }];

        [[self socket] onObjectiveC:@"currentAmount" callback:^(NSArray* data, void (^ack)(NSArray*)) {
            double cur = [[data objectAtIndex:0] floatValue];

            [[self socket] emitWithAck:@"canUpdate" withItems:@[@(cur)]](0, ^(NSArray* data) {
                [[self socket] emit:@"update" withItems:@[@{@"amount": @(cur + 2.50)}]];
            });

            ack(@[@"Got your ack, ", @"dude"]);
            NSLog(@"Sent ack");
        }];
    }

    return self;
}

- (void)connect
{
    [[self socket] connectWithTimeoutAfter: 5 withTimeoutHandler: ^{
        NSLog(@"Client failed to connect");
    }];
}

- (void)sendMessage:(NSString*)msg
{
    for (int i = 0; i < 1004000; i++) {
        [[self socket] emit:@"test" withItems:@[msg]];
    }
}

@end
nuclearace commented 9 years ago

Also look at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

danipralea commented 9 years ago

Yes, I tried again. Now it's working. Thank you!