Code Monkey home page Code Monkey logo

eventsource's People

Contributors

neilco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

eventsource's Issues

Received data splits if is too long

Extracted from #9 (comment)

My server returns data in single line. But if it is very large (a few dozen screens if use line-breaks, I may measure if needed), looks like URLSession calls didReceiveData with parts of payload. I found it here. So payload just comes broken.

NSURLRequest timeout

I was having an issue with the event source disconnecting at regular intervals and determined that it was because the underlying NSURLRequest hadn't had its timeoutInterval set. After changing [NSURLRequest requestWithURL:] to [NSURLRequest requestWithURL: cachePolicy: timeoutInterval:], the connection stays open as expected. Perhaps it would make sense to change [EventSource initWithURL:] to [EventSource initWithURL: timeoutInterval:]?

If server send a lot of event handler will not get call

If server send a lot of event handler will not get call, it will stuck on first for loop. Here is my hot-fix code.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    __block NSString *eventString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    if ([eventString hasSuffix:@"\n\n"]) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            NSArray* eventArray = [eventString componentsSeparatedByString:@"\n\n"];
            for (NSString* sigleEventString in eventArray) {
                NSArray *components = [sigleEventString componentsSeparatedByString:@"\n"] ;
                Event *e = [Event new];
                e.readyState = kEventStateOpen;

                for (NSString *component in components) {
                    NSUInteger location = [component rangeOfString:@":"].location;
                    if (location != NSNotFound) {
                        NSString *dataPath = [component substringFromIndex:location+1];
                        dataPath = [dataPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                        if ([component hasPrefix:@"id:"]) {
                            e.id = dataPath;
                        } else if ([component hasPrefix:@"event:"]) {
                            e.event = dataPath;
                        } else if ([component hasPrefix:@"data:"]) {
                            e.data = dataPath;
                        }

                    }
                }

                if (e.event != nil) {
                    NSArray *namedEventhandlers = listeners[e.event];
                    for (EventSourceEventHandler handler in namedEventhandlers) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            handler(e);
                        });
                    }
                }
            }
        });
    }
}

Continuously tries to reconnect.

Hi,
If I attempt to connect to a stream which I know will fail (Incorrect auth) the onError: method is called.
In this method I call [self.source close]; however I keep receiving onError: callbacks as if it's still trying to connect.

Calling [self.source close]; should surely stop any attempts to reconnect?

Calling [self.source close]; on any connections that are open, successfully closes them.

watchOS Support

Any plan to support the watchOS as well in the near future?

Firebase observer using REST Api

I am building IOS application using firebase REST API. From the doc (Ref: https://firebase.google.com/docs/reference/rest/database/) it says that we can Stream from the REST API.I am not able to listen firebase events.When I run the app Only onOpen callback is called. When i changed the child value on firebase nothing happened on callback methods. Can you suggest me the way to observe the events?
I tried the following code:

    Auth.auth().currentUser?.getIDTokenForcingRefresh(true, completion: { (token, error) in
    let server : String =  "https://project-XXXXX.firebaseio.com/.json?auth=\(token!)"

    let eventSource: EventSource = EventSource(url: server)
    eventSource.onOpen {
        // When opened
        debugPrint("eventSource open")
    }

    eventSource.onError { (error) in
        // When errors
        debugPrint("error = \(error?.localizedDescription)")
    }
    eventSource.onMessage { (id, event, data) in
         debugPrint("data = \(data)")
        // Here you get an event without event name!
    }

    eventSource.addEventListener("child_added") { (id, event, data) in
           debugPrint("data = \(data)")
        // Here you get an event 'event-name'
    }
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.