Created
November 14, 2013 12:30
-
-
Save seanbollin/7466007 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CDVSocketBridge.m | |
// Simple Beauty, Inc. | |
// | |
// Created by Sean Bollin on 11/11/13. | |
// | |
// | |
#import "CDVSocketBridge.h" | |
@implementation CDVSocketBridge | |
@synthesize cordovaCommand; | |
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { | |
switch (streamEvent) { | |
case NSStreamEventNone: | |
NSLog(@"CDVSocketBridge Event Received: NSStreamEventNone"); | |
case NSStreamEventOpenCompleted: | |
NSLog(@"CDVSocketBridge Event Received: NSStreamEventOpenCompleted"); | |
break; | |
case NSStreamEventHasBytesAvailable: | |
NSLog(@"CDVSocketBridge Event Received: NSStreamEventHasBytesAvailable"); | |
if (theStream == inputStream) { | |
uint8_t buffer[1024]; | |
int len; | |
while([inputStream hasBytesAvailable]) { | |
len = [inputStream read:buffer maxLength:sizeof(buffer)]; | |
if (len > 0) { | |
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; | |
if (nil != output) { | |
NSLog(@"CDVSocketBridge Server Output: %@", output); | |
[self receiveMessage:output]; | |
} | |
} | |
} | |
} | |
break; | |
case NSStreamEventHasSpaceAvailable: | |
NSLog(@"CDVSocketBridge Event Received: NSStreamEventHasSpaceAvailable"); | |
break; | |
case NSStreamEventErrorOccurred: | |
NSLog(@"CDVSocketBridge Event Received: NSStreamEventErrorOccurred"); | |
break; | |
case NSStreamEventEndEncountered: | |
NSLog(@"CDVSocketBridge Event Received: NSStreamEventEndEncountered"); | |
break; | |
default: | |
NSLog(@"CDVSocketBridge Event Received: Unknown event"); | |
} | |
} | |
- (void)initNetworkCommunication | |
{ | |
CFReadStreamRef readStream; | |
CFWriteStreamRef writeStream; | |
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"127.0.0.1", 5555, &readStream, &writeStream); | |
inputStream = (NSInputStream *)readStream; | |
outputStream = (NSOutputStream *)writeStream; | |
[inputStream setDelegate:self]; | |
[outputStream setDelegate:self]; | |
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[inputStream open]; | |
[outputStream open]; | |
} | |
- (void)onReset | |
{ | |
//[inputStream close]; | |
//[outputStream close]; | |
} | |
- (void)onAppTerminate | |
{ | |
//[inputStream close]; | |
//[outputStream close]; | |
} | |
- (void)receiveMessage:(NSString*)msg | |
{ | |
CDVPluginResult* pluginResult = nil; | |
if (msg != nil && [msg length] > 0) { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg]; | |
} else { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; | |
} | |
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.cordovaCommand.callbackId]; | |
} | |
- (void)pluginInitialize | |
{ | |
[self initNetworkCommunication]; | |
} | |
- (void)sendMessage:(CDVInvokedUrlCommand*)command | |
{ | |
self.cordovaCommand = command; | |
NSString* msg = [command.arguments objectAtIndex:0]; | |
msg = [NSString stringWithFormat:@"%@\n", msg]; | |
NSData *data = [[NSData alloc] initWithData:[msg dataUsingEncoding:NSASCIIStringEncoding]]; | |
[outputStream write:[data bytes] maxLength:[data length]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment