-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
Samples/Playgrounds/SimpleAuthSample.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Cocoa | ||
import PlaygroundSupport | ||
import DXFeedFramework | ||
|
||
// Empty Event Listener with handler | ||
class Listener: DXEventListener, Hashable { | ||
|
||
static func == (lhs: Listener, rhs: Listener) -> Bool { | ||
lhs === rhs | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine("\(self):\(stringReference(self))") | ||
} | ||
var callback: ([MarketEvent]) -> Void = { _ in } | ||
|
||
func receiveEvents(_ events: [MarketEvent]) { | ||
self.callback(events) | ||
} | ||
|
||
init(overrides: (Listener) -> Listener) { | ||
_ = overrides(self) | ||
} | ||
} | ||
|
||
// Empty Endpoint Listener with handler | ||
class EndpoointStateListener: DXEndpointListener, Hashable { | ||
func endpointDidChangeState(old: DXFeedFramework.DXEndpointState, new: DXFeedFramework.DXEndpointState) { | ||
callback(old, new) | ||
} | ||
|
||
static func == (lhs: EndpoointStateListener, rhs: EndpoointStateListener) -> Bool { | ||
lhs === rhs | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine("\(self):\(stringReference(self))") | ||
} | ||
var callback: (DXEndpointState, DXEndpointState) -> Void = { _,_ in } | ||
|
||
init(overrides: (EndpoointStateListener) -> EndpoointStateListener) { | ||
_ = overrides(self) | ||
} | ||
} | ||
|
||
let address = "demo.dxfeed.com:7300"; | ||
|
||
func updateTokenAndReconnect() { | ||
try? DXEndpoint.getInstance().connect("\(address)[login=entitle:\(generateToken())]") | ||
} | ||
|
||
func generateToken() -> String { | ||
let length = Int.random(in: 4...10) | ||
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
return String((0..<length).map { _ in letters.randomElement()! }) | ||
} | ||
|
||
|
||
// Demonstrates how to connect to endpoint requires authentication token, subscribe to market data events, | ||
// and handle periodic token updates. | ||
let endpoint = try DXEndpoint.getInstance() | ||
// Add a listener for state changes to the default application-wide singleton instance of DXEndpoint. | ||
let stateListener = EndpoointStateListener { listener in | ||
listener.callback = { old, new in | ||
print("Connection state changed: \(old) -> \(new)") | ||
} | ||
return listener | ||
} | ||
endpoint.add(listener: stateListener) | ||
|
||
// Set up a timer to periodically update the token and reconnect every 10 seconds. | ||
// The first connection will be made immediately. | ||
// After reconnection, all existing subscriptions will be re-subscribed automatically. | ||
Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { timer in | ||
updateTokenAndReconnect() | ||
}.fire() | ||
|
||
// Create a subscription for Quote events. | ||
let subscriptionQuote = try endpoint.getFeed()?.createSubscription(Quote.self) | ||
// Listener must be attached before symbols are added. | ||
let listener = Listener { listener in | ||
listener.callback = { events in | ||
// Event listener that prints each received event. | ||
events.forEach { event in | ||
print(event.toString()) | ||
} | ||
} | ||
return listener | ||
} | ||
try subscriptionQuote?.add(listener: listener) | ||
|
||
// Add the specified symbol to the subscription. | ||
try subscriptionQuote?.addSymbols("ETH/USD:GDAX") | ||
|
||
// Keep the application running indefinitely. | ||
PlaygroundPage.current.needsIndefiniteExecution = true | ||
|
||
// to finish execution run this line | ||
PlaygroundPage.current.finishExecution() |
4 changes: 4 additions & 0 deletions
4
Samples/Playgrounds/SimpleAuthSample.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |