neilco / eventsource Goto Github PK
View Code? Open in Web Editor NEWServer-Sent Events for iOS and Mac
License: MIT License
Server-Sent Events for iOS and Mac
License: MIT License
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.
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:]?
When I am trying to access the event source , I am getting the json breaking or not receiving proper json.
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);
});
}
}
}
});
}
}
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.
Any plan to support the watchOS as well in the near future?
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'
}
})
Thanks for your good library. EventSource has memory leak issue.
I read this post in stackoverflow.
http://stackoverflow.com/questions/28223345/memory-leak-when-using-nsurlsession-downloadtaskwithurl
http://stackoverflow.com/questions/21554987/nsurlsession-memory-leaks-occur-when-using-web-services-in-ios
Can you please check it and update library ?
Can you please update pod spec into 1.0.4 ?
It's trying to download 1.0.2.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.