Skip to content

Ability to send only to specific connection(s) #4

@hongkongkiwi

Description

@hongkongkiwi

Depending on how the server is used, it sometimes only needs to deal with specific connections. For example, disconnect a specific connection or send data to a specific connection(s).

In my case I was having the server manage sessions which clients could join/leave. So I added the following (very simple) code.

// MBWebSocketServer.h
- (void)disconnectConnection:(GCDAsyncSocket *)aConnection;
- (void)sendToConnection:(GCDAsyncSocket *)aConnection data:(id)object;
- (void)sendToConnections:(NSArray *)aCollectionsArray data:(id)object;

// MBWebSocketServer.m
- (void)sendToConnection:(GCDAsyncSocket *)aConnection data:(id)object {
    NSData *payload = [object webSocketFrameData];
    [aConnection writeData:payload withTimeout:-1 tag:3];
}

- (void)sendToConnections:(NSArray *)aCollectionsArray data:(id)object {
    id payload = [object webSocketFrameData];
    for (GCDAsyncSocket *connection in aCollectionsArray) {
        [connection writeData:payload withTimeout:-1 tag:3];
    }
}

- (void)disconnectConnection:(GCDAsyncSocket *)aConnection {
    [aConnection disconnect];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions