Last active
November 16, 2024 05:31
-
-
Save quyendang/e2a20827f219501f0b25395353e5f8ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
import Starscream | |
import UIKit | |
class GetGrassMiner: WebSocketDelegate { | |
func didReceive(event: Starscream.WebSocketEvent, client: Starscream.WebSocketClient) { | |
switch event { | |
case .connected(let headers): | |
print("websocket is connected: \(headers)") | |
isConnected = true | |
sendPing() | |
case .disconnected(let reason, let code): | |
print("websocket is disconnected: \(reason) with code: \(code)") | |
isConnected = false | |
connect() | |
case .text(let text): | |
print("Received text: \(text)") | |
let message = try? JSONSerialization.jsonObject(with: text.data(using: .utf8)!, options: []) as? [String: Any] | |
if let action = message?["action"] as? String, let id = message?["id"] as? String { | |
switch action { | |
case "AUTH": | |
sendAuthResponse(id) | |
case "PONG": | |
sendPongResponse(id) | |
default: | |
break | |
} | |
} | |
case .binary(let data): | |
print("Received data: \(data.count)") | |
case .ping(let ping): | |
let str = String(decoding: ping!, as: UTF8.self) | |
print("ping \(str)") | |
case .pong(let pongResponse): | |
let str = String(decoding: pongResponse!, as: UTF8.self) | |
print("pong \(str)") | |
case .viabilityChanged(let state): | |
print("State \(state)") | |
case .reconnectSuggested(_): | |
isConnected = false | |
self.webSocket?.connect() | |
case .cancelled: | |
isConnected = false | |
self.webSocket?.connect() | |
case .error(let error): | |
print(error?.localizedDescription) | |
isConnected = false | |
self.webSocket?.connect() | |
case .peerClosed: | |
isConnected = false | |
self.webSocket?.connect() | |
} | |
} | |
private var webSocket: WebSocket? | |
private var timer: Timer? | |
private let userId: String | |
private let deviceId: UUID | |
private var isConnected = false | |
init(userId: String) { | |
self.userId = userId | |
self.deviceId = UIDevice.current.identifierForVendor! | |
self.timer = Timer.scheduledTimer(timeInterval: 20.0, target: self, selector: #selector(ping), userInfo: nil, repeats: true) | |
} | |
@objc func ping() { | |
sendPing() | |
} | |
func connect() { | |
let request = URLRequest(url: URL(string: "wss://proxy.wynd.network:4650")!) | |
let webSocket = WebSocket(request: request) | |
webSocket.delegate = self | |
webSocket.connect() | |
self.webSocket = webSocket | |
} | |
private func sendPing() { | |
let sendMessage = [ | |
"id": UUID().uuidString.lowercased(), | |
"version": "1.0.0", | |
"action": "PING", | |
"data": [:] | |
] as [String: Any] | |
let jsonData = try? JSONSerialization.data(withJSONObject: sendMessage, options: []) | |
//webSocket?.write(data: jsonData!) | |
webSocket?.write(ping: jsonData!, completion: { | |
print("Ping Success") | |
}) | |
} | |
private func sendAuthResponse(_ id: String) { | |
let authResponse = [ | |
"id": id, | |
"origin_action": "AUTH", | |
"result": [ | |
"browser_id": deviceId.uuidString.lowercased(), | |
"user_id": userId, | |
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36", | |
"timestamp": Int(Date().timeIntervalSince1970), | |
"device_type": "extension", | |
"version": "3.3.2" | |
] | |
] as [String: Any] | |
let jsonData = try? JSONSerialization.data(withJSONObject: authResponse, options: []) | |
//webSocket?.write(data: jsonData!) | |
webSocket?.write(data: jsonData!, completion: { | |
print("Auth: \(authResponse)") | |
}) | |
} | |
private func sendPongResponse(_ id: String) { | |
let pongResponse = [ | |
"id": id, | |
"origin_action": "PONG" | |
] as [String: Any] | |
let jsonData = try? JSONSerialization.data(withJSONObject: pongResponse, options: []) | |
//webSocket?.write(data: jsonData!) | |
webSocket?.write(pong: jsonData!, completion: { | |
print("Pong Success") | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cài thư viên Starscream thông qua Swift Package Manager:
https://github.com/daltoniam/Starscream.git
Bỏ code sau vào :
let proxy = WebSocketManager(userId: "205c5d48-31aa-43c8-8559-3e6bc28e217e")
proxy.connect()