SRConnection Class Reference
| Inherits from | NSObject | 
| Declared in | SRConnection.h | 
Overview
An SRConnection object provides support to open a persistent connection with a SignalR Server.
SRConnection’s delegate methods—defined by the SRConnectionDelegate protocol allows an object to receive informational callbacks when events occur on the SRConnection object
Tasks
Properties
- 
	
		
startedA block to be called when the connection’s underlying transport is
propertyinitializedthe first time - 
	
		
receivedA block to be called when the connection’s underlying transport receives new data
property - 
	
		
errorA block to be called when the connection’s underlying transport receives a error
property - 
	
		
closedA block to be called when the connection’s underlying transport is closed
property - 
	
		
reconnectedA block to be called when the connection’s underlying transport is reconnected
property - 
	
		
credentialsThe authentication credential to be applied to each request Support is limited to HTTP Basic Authentication
property - 
	
		
groupsA
propertyNSMutableArrayof the connected groups - 
	
		
sendingA block to be called prior to sending data
property - 
	
		
urlThe endpoint at which the connection is initialized to
property - 
	
		
activeA
propertyBOOLrepresenting the status of the connection when TRUE the connection has been started - 
	
		
messageIdAn
propertyNSNumberrepresenting the current message id - 
	
		
connectionIdAn
propertyNSStringrepresenting the current connectionId established during negotiation - 
	
		
itemsAn
propertyNSMutableDictionarycontaining objects that need to be persisted by the underlying transport for example the last httprequest. - 
	
		
queryStringAn
propertyNSStringrepresenting the custom query string to be applied to each request - 
	
		
initializedA
propertyBOOLrepresenting the status of the connection underlying transport when TRUE the connection’s underlying transport has been started - 
	
		
headersAn
propertyNSMutableDictionaryrepresenting the headers to be applied to each request - 
	
		
delegateThe object that acts as the delegate of the receiving
propertySRConnection. 
Initializing an SRConnection Object
- 
	
		
+ connectionWithURL:A convenience method for initWithURL:(NSString *)url;
 - 
	
		
+ connectionWithURL:query:A convenience method for initWithURL:(NSString )url query:(NSDictionary )queryString;
 - 
	
		
+ connectionWithURL:queryString:A convenience method for initWithURL:(NSString )url queryString:(NSString )queryString;;
 - 
	
		
– initWithURL:Initializes a new
SRConnectionobject at the specified URL - 
	
		
– initWithURL:query:Initializes a new
SRConnectionobject at the specified URL - 
	
		
– initWithURL:queryString:Initializes a new
SRConnectionobject at the specified URL 
Connection Management
- 
	
		
– startStarts the connection using SRAutoTransport
 - 
	
		
– start:Starts the connection
 - 
	
		
– negotiatePerfromed prior to starting the underlying transport, a negoitate establishes the initial connection with the server by making a request to url/negotiate a sucessful response returns an SRNegotiationResponse object which establishes the connectionId at which time the underlying transport will be started. dispatches the opened event to either the
SRConnectionDelegateby calling [self.delegate SRConnectionDidOpen:self]; or to the self.started callback once the transport is successfully initialized - 
	
		
– stopStops the connection dispatches the closed event to either the
SRConnectionDelegateby calling [self.delegate SRConnectionDidClose:self]; or to the self.closed callback setsactiveandinitializedto NO 
Sending Data
- 
	
		
– send:Sends data over the connection. calls [self send:message continueWith:nil];
 - 
	
		
– send:continueWith:Sends data over the connection.
 
Receiving Data
- 
	
		
– didReceiveData:Called when the connection receives new data dispatches the response to either the
SRConnectionDelegateby calling [self.delegate SRConnection:self didReceiveData:message]; or to the self.received callback - 
	
		
– didReceiveError:Called when the connection receives an error dispatches the error to either the
SRConnectionDelegateby calling [self.delegate SRConnection:self didReceiveError:ex]; or to the self.error callback - 
	
		
– didReconnectCalled when the connection reconnects after a server side timeout dispatches the reconnect to either the
SRConnectionDelegateby calling [self.delegate SRConnectionDidReconnect:self]; or to the self.reconnected callback 
Preparing Requests
- 
	
		
– addValue:forHTTPHeaderField:Adds an HTTP header to the receiver’s HTTP header dictionary.
 - 
	
		
– prepareRequest:Sets the user agent, crediential information and other relevant header values on each connection
 - 
	
		
– createUserAgentString:Generates the client UserAgent header field
 
Extensions Methods
- 
	
		
– getValue:An extension method for accessing objects belonging to [SRConnection items]
 
Properties
active
A BOOL representing the status of the connection
when TRUE the connection has been started
@property (assign, nonatomic, readonly, getter=isActive) BOOL activeDeclared In
SRConnection.hclosed
A block to be called when the connection’s underlying transport is closed
@property (copy) onClosed closedDeclared In
SRConnection.hconnectionId
An NSString representing the current connectionId established during negotiation
@property (strong, nonatomic, readwrite) NSString *connectionIdDeclared In
SRConnection.hcredentials
The authentication credential to be applied to each request Support is limited to HTTP Basic Authentication
@property (strong, nonatomic, readwrite) NSURLCredential *credentialsDeclared In
SRConnection.hdelegate
The object that acts as the delegate of the receiving SRConnection.
@property (nonatomic, assign) id<> delegateDeclared In
SRConnection.herror
A block to be called when the connection’s underlying transport receives a error
@property (copy) onError errorDeclared In
SRConnection.hgroups
A NSMutableArray of the connected groups
@property (strong, nonatomic, readwrite) NSMutableArray *groupsDeclared In
SRConnection.hheaders
An NSMutableDictionary representing the headers to be applied to each request
@property (strong, nonatomic, readonly) NSMutableDictionary *headersDeclared In
SRConnection.hinitialized
A BOOL representing the status of the connection underlying transport
when TRUE the connection’s underlying transport has been started
@property (assign, nonatomic, readonly) BOOL initializedDeclared In
SRConnection.hitems
An NSMutableDictionary containing objects that need to be persisted by the underlying transport for example the last httprequest.
@property (strong, nonatomic, readwrite) NSMutableDictionary *itemsDeclared In
SRConnection.hmessageId
An NSNumber representing the current message id
@property (strong, nonatomic, readwrite) NSNumber *messageIdDeclared In
SRConnection.hqueryString
An NSString representing the custom query string to be applied to each request
@property (strong, nonatomic, readonly) NSString *queryStringDeclared In
SRConnection.hreceived
A block to be called when the connection’s underlying transport receives new data
@property (copy) onReceived receivedDeclared In
SRConnection.hreconnected
A block to be called when the connection’s underlying transport is reconnected
@property (copy) onReconnected reconnectedDeclared In
SRConnection.hsending
A block to be called prior to sending data
@property (copy) onSending sendingDeclared In
SRConnection.hstarted
A block to be called when the connection’s underlying transport is initialized the first time
@property (copy) onStarted startedDeclared In
SRConnection.hurl
The endpoint at which the connection is initialized to
@property (strong, nonatomic, readwrite) NSString *urlDeclared In
SRConnection.hClass Methods
connectionWithURL:
A convenience method for initWithURL:(NSString *)url;
+ (SRConnection *)connectionWithURL:(NSString *)URLParameters
- URL
 the endpoint to initialize the new connection to
Return Value
an SRConnection object
Discussion
SRConnection *connection = [SRConnection connectionWithURL:@“http://mysite/echo”]http://mysite/echo“];
Declared In
SRConnection.hconnectionWithURL:query:
A convenience method for initWithURL:(NSString )url query:(NSDictionary )queryString;
+ (SRConnection *)connectionWithURL:(NSString *)url query:(NSDictionary *)queryStringParameters
- url
 the endpoint to initialize the new connection to
- queryString
 an
NSDictionaryrepresentation of a custom query string to be appended to theSRConnectionendpoint
Return Value
an SRConnection object
Discussion
SRConnection *connection = [SRConnection connectionWithURL:@“http://mysite/echo”]http://mysite/echo“];
Declared In
SRConnection.hconnectionWithURL:queryString:
A convenience method for initWithURL:(NSString )url queryString:(NSString )queryString;;
+ (SRConnection *)connectionWithURL:(NSString *)url queryString:(NSString *)queryStringParameters
- url
 the endpoint to initialize the new connection to
- queryString
 an
NSStringrepresentation of a custom query string to be appended to theSRConnectionendpoint
Return Value
an SRConnection object
Discussion
Important Url cannot contain a QueryString directly. Namely the string should not contain the prefix ‘?’ It is recommended that (SRConnection )connectionWithURL:(NSString )url query:(NSDictionary *)queryString; is used instead
Declared In
SRConnection.hInstance Methods
addValue:forHTTPHeaderField:
Adds an HTTP header to the receiver’s HTTP header dictionary.
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)fieldParameters
- value
 The value for the header field.
- field
 the name of the header field.
Declared In
SRConnection.hcreateUserAgentString:
Generates the client UserAgent header field
- (NSString *)createUserAgentString:(NSString *)clientParameters
- client
 the client name
Return Value
The user agent will be of the form client/version (DeviceModel/DeviceVersion)
Declared In
SRConnection.hdidReceiveData:
Called when the connection receives new data
dispatches the response to either the SRConnectionDelegate by calling [self.delegate SRConnection:self didReceiveData:message];
or to the self.received callback
- (void)didReceiveData:(NSString *)dataParameters
- data
 the data received from the server
Declared In
SRConnection.hdidReceiveError:
Called when the connection receives an error
dispatches the error to either the SRConnectionDelegate by calling [self.delegate SRConnection:self didReceiveError:ex];
or to the self.error callback
- (void)didReceiveError:(NSError *)exDeclared In
SRConnection.hdidReconnect
Called when the connection reconnects after a server side timeout
dispatches the reconnect to either the SRConnectionDelegate by calling [self.delegate SRConnectionDidReconnect:self];
or to the self.reconnected callback
- (void)didReconnectDeclared In
SRConnection.hgetValue:
An extension method for accessing objects belonging to [SRConnection items]
- (id)getValue:(NSString *)keyParameters
- key
 he key for which to return the corresponding value.
Return Value
Returns the value associated with a given key.
Declared In
SRConnectionExtensions.hinitWithURL:
Initializes a new SRConnection object at the specified URL
- (id)initWithURL:(NSString *)urlParameters
- url
 the endpoint to initialize the new connection to
Return Value
an SRConnection object
Discussion
SRConnection *connection = [[SRConnection alloc] initWithURL:@“http://mysite/echo”];
Declared In
SRConnection.hinitWithURL:query:
Initializes a new SRConnection object at the specified URL
- (id)initWithURL:(NSString *)url query:(NSDictionary *)queryStringParameters
- url
 the endpoint to initialize the new connection to
- queryString
 an
NSDictionaryrepresentation of a custom query string to be appended to theSRConnectionendpoint
Return Value
an SRConnection object
Declared In
SRConnection.hinitWithURL:queryString:
Initializes a new SRConnection object at the specified URL
- (id)initWithURL:(NSString *)url queryString:(NSString *)queryStringParameters
- url
 the endpoint to initialize the new connection to
- queryString
 an
NSStringrepresentation of a custom query string to be appended to theSRConnectionendpoint
Return Value
an SRConnection object
Discussion
Important Url cannot contain a QueryString directly. Namely the string should not contain the prefix ‘?’ It is recommended that (id)initWithURL:(NSString )url query:(NSDictionary )queryString;; is used instead
Declared In
SRConnection.hnegotiate
Perfromed prior to starting the underlying transport, a negoitate establishes the initial connection with the server
by making a request to url/negotiate a sucessful response returns an SRNegotiationResponse object which establishes the connectionId
at which time the underlying transport will be started.
dispatches the opened event to either the SRConnectionDelegate by calling [self.delegate SRConnectionDidOpen:self];
or to the self.started callback once the transport is successfully initialized
- (void)negotiateDeclared In
SRConnection.hprepareRequest:
Sets the user agent, crediential information and other relevant header values on each connection
- (void)prepareRequest:(id)requestParameters
- request
 The
NSMutableURLRequestthat will be sent to the server
Declared In
SRConnection.hsend:
Sends data over the connection. calls [self send:message continueWith:nil];
- (void)send:(NSString *)messageParameters
- message
 the message to send
Declared In
SRConnection.hsend:continueWith:
Sends data over the connection.
- (void)send:(NSString *)message continueWith:(void ( ^ ) ( id response ))blockParameters
- message
 the message to send
- block
 the callback to be called once the message is sent.
Discussion
Important Start must be called before sending data
Declared In
SRConnection.hstart
Starts the connection using SRAutoTransport
- (void)startDiscussion
sets active to YES
Declared In
SRConnection.hstart:
Starts the connection
- (void)start:(id<SRClientTransport>)transportParameters
- transport
 the transport to use for the connection
Discussion
sets active to YES
Declared In
SRConnection.hstop
Stops the connection
dispatches the closed event to either the SRConnectionDelegate by calling [self.delegate SRConnectionDidClose:self];
or to the self.closed callback
sets active and initialized to NO
- (void)stopDeclared In
SRConnection.h