Skip to content

Commit d21a65e

Browse files
committed
Add rawEmitView. Implements socketio#992
1 parent 4564aeb commit d21a65e

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

Source/SocketIO/Client/SocketBinaryView.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import Foundation
77
/// Class that gives a backwards compatible way to cause an emit not to recursively check for Data objects.
88
///
99
/// Usage:
10+
///
1011
/// ```swift
11-
/// socket.binary(false).emit("myEvent", myObject)
12+
/// socket.rawEmitView.emit("myEvent", myObject)
1213
/// ```
1314
public final class SocketBinaryView : NSObject {
1415
private unowned let socket: SocketIOClient
15-
private let binary: Bool
1616

17-
init(socket: SocketIOClient, binary: Bool) {
17+
init(socket: SocketIOClient) {
1818
self.socket = socket
19-
self.binary = binary
2019
}
2120

2221
/// Send an event to the server, with optional data items.
@@ -48,7 +47,7 @@ public final class SocketBinaryView : NSObject {
4847
return
4948
}
5049

51-
socket.emit([event] + items, binary: binary)
50+
socket.emit([event] + items, binary: false)
5251
}
5352

5453
/// Sends a message to the server, requesting an ack.
@@ -101,6 +100,6 @@ public final class SocketBinaryView : NSObject {
101100
/// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
102101
@objc
103102
open func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback {
104-
return socket.createOnAck([event] + items, binary: binary)
103+
return socket.createOnAck([event] + items, binary: false)
105104
}
106105
}

Source/SocketIO/Client/SocketIOClient.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
6767
@objc
6868
public private(set) weak var manager: SocketManagerSpec?
6969

70+
/// A view into this socket where emits do not check for binary data.
71+
///
72+
/// Usage:
73+
///
74+
/// ```swift
75+
/// socket.rawEmitView.emit("myEvent", myObject)
76+
/// ```
77+
///
78+
/// **NOTE**: It is not safe to hold on to this view beyond the life of the socket.
79+
@objc
80+
public private(set) lazy var rawEmitView = SocketBinaryView(socket: self)
81+
7082
/// The status of this client.
7183
@objc
7284
public private(set) var status = SocketIOStatus.notConnected {

Source/SocketIO/Client/SocketIOClientSpec.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public protocol SocketIOClientSpec : class {
4343
/// **Must** start with a `/`.
4444
var nsp: String { get }
4545

46+
/// A view into this socket where emits do not check for binary data.
47+
///
48+
/// Usage:
49+
///
50+
/// ```swift
51+
/// socket.rawEmitView.emit("myEvent", myObject)
52+
/// ```
53+
///
54+
/// **NOTE**: It is not safe to hold on to this view beyond the life of the socket.
55+
var rawEmitView: SocketBinaryView { get }
56+
4657
/// The status of this client.
4758
var status: SocketIOStatus { get }
4859

Tests/TestSocketIOObjc/SocketObjectiveCTest.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ - (void)testEmitSyntax {
6666
[self.socket emit:@"testEmit" with:@[@YES]];
6767
}
6868

69+
- (void)testRawEmitSyntax {
70+
[[self.socket rawEmitView] emit:@"myEvent" with:@[@1]];
71+
}
72+
6973
- (void)testEmitWithAckSyntax {
7074
[[self.socket emitWithAck:@"testAckEmit" with:@[@YES]] timingOutAfter:0 callback:^(NSArray* data) { }];
7175
}

0 commit comments

Comments
 (0)