I am playing with a simple command-line app:
$ swift package init --type=executable
After adding Starscream as a dependency, I added the following code in main.swift
import Foundation
import Starscream
let echoURL = URL(string: "ws://echo.websocket.org/")!
class WsClient: WebSocketDelegate {
let socket: WebSocket
init() {
socket = WebSocket(url: echoURL)
socket.delegate = self
}
func connect() {
print("Connecting")
socket.connect()
}
func websocketDidConnect(socket: WebSocket) {
print("Connected")
socket.write(string: "test") {
print("Sent")
}
}
func websocketDidReceiveData(socket: WebSocket, data: Data) {
}
func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
print("Disconnect")
print(error)
}
func websocketDidReceiveMessage(socket: WebSocket, text: String) {
}
}
let client = WsClient()
client.connect()
sleep(10)
I build the app and run it.
$ swift build
$ ./build/.../TestApp
However the delegate methods are not called, i.e. I see "Connecting" printed but not "Connected" (or "Disconnected".
Any help appreciated.
PS: I am running on latest macOS with Swift 3.0
The issue is using sleep. That blocks the main runloop from processing any of the delegate request that come back to it. Change the sleep to:
while(true) {
RunLoop.current.run(until: Date())
usleep(10)
}
This will achieve the "blocking" and keep the app from closing right on start, but still allow the runloop to process delegate callbacks.
Full code for clarity:
import Foundation
import Starscream
let echoURL = URL(string: "ws://echo.websocket.org/")!
class WsClient: WebSocketDelegate {
let socket: WebSocket
init() {
socket = WebSocket(url: echoURL)
socket.delegate = self
}
func connect() {
print("Connecting")
socket.connect()
}
func websocketDidConnect(socket: WebSocket) {
print("Connected")
socket.write(string: "test") {
print("Sent")
}
}
func websocketDidReceiveData(socket: WebSocket, data: Data) {
}
func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
print("Disconnect")
print(error)
}
func websocketDidReceiveMessage(socket: WebSocket, text: String) {
print("Got: \(text)")
}
}
let client = WsClient()
client.connect()
while(true) {
RunLoop.current.run(until: Date())
usleep(10)
}
Duh!
Working with single-threaded languages for a long-time does this to you :)
Thank you!
On Mon, Sep 26, 2016 at 7:47 AM Dalton [email protected] wrote:
Closed #254 https://github.com/daltoniam/Starscream/issues/254.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/daltoniam/Starscream/issues/254#event-801763669, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AACR3m-1XTEwc5j2TsIx_D8TEfRb94rhks5qt05tgaJpZM4KF3qI
.
@gmosx how did you get around missing dynamic link refs when building and running the executable? I'm getting these issues, and I believe it's because Swift has no good way of dealing with dynamic frameworks for commandline apps. Coacocapods introduced a way to specify a framework as static, but Starscream doesn't appear to conform in its Podspec
Most helpful comment
Full code for clarity: