在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:daltoniam/Starscream开源软件地址:https://github.com/daltoniam/Starscream开源编程语言:Swift 97.8%开源软件介绍:Starscream is a conforming WebSocket (RFC 6455) library in Swift. Features
Import the frameworkFirst thing is to import the framework. See the Installation instructions on how to add the framework to your project. import Starscream Connect to the WebSocket ServerOnce imported, you can open a connection to your WebSocket server. Note that var request = URLRequest(url: URL(string: "http://localhost:8080")!)
request.timeoutInterval = 5
socket = WebSocket(request: request)
socket.delegate = self
socket.connect() After you are connected, there is either a delegate or closure you can use for process WebSocket events. Receiving data from a WebSocket
func didReceive(event: WebSocketEvent, client: WebSocket) {
switch event {
case .connected(let headers):
isConnected = true
print("websocket is connected: \(headers)")
case .disconnected(let reason, let code):
isConnected = false
print("websocket is disconnected: \(reason) with code: \(code)")
case .text(let string):
print("Received text: \(string)")
case .binary(let data):
print("Received data: \(data.count)")
case .ping(_):
break
case .pong(_):
break
case .viabilityChanged(_):
break
case .reconnectSuggested(_):
break
case .cancelled:
isConnected = false
case .error(let error):
isConnected = false
handleError(error)
}
} The closure of this would be: socket.onEvent = { event in
switch event {
// handle events just like above...
}
} Writing to a WebSocketwrite a binary frameThe writeData method gives you a simple way to send socket.write(data: data) //write some Data over the socket! write a string frameThe writeString method is the same as writeData, but sends text/string. socket.write(string: "Hi Server!") //example on how to write text over the socket! write a ping frameThe writePing method is the same as write, but sends a ping control frame. socket.write(ping: Data()) //example on how to write a ping control frame over the socket! write a pong framethe writePong method is the same as writePing, but sends a pong control frame. socket.write(pong: Data()) //example on how to write a pong control frame over the socket! Starscream will automatically respond to incoming However if for some reason you need to control this process you can turn off the automatic socket.respondToPingWithPong = false //Do not automaticaly respond to incoming pings with pongs. In most cases you will not need to do this. disconnectThe disconnect method does what you would expect and closes the socket. socket.disconnect() The disconnect method can also send a custom close code if desired. socket.disconnect(closeCode: CloseCode.normal.rawValue) Custom Headers, Protocols and TimeoutYou can override the default websocket headers, add your own custom ones and set a timeout: var request = URLRequest(url: URL(string: "ws://localhost:8080/")!)
request.timeoutInterval = 5 // Sets the timeout for the connection
request.setValue("someother protocols", forHTTPHeaderField: "Sec-WebSocket-Protocol")
request.setValue("14", forHTTPHeaderField: "Sec-WebSocket-Version")
request.setValue("chat,superchat", forHTTPHeaderField: "Sec-WebSocket-Protocol")
request.setValue("Everything is Awesome!", forHTTPHeaderField: "My-Awesome-Header")
let socket = WebSocket(request: request) SSL PinningSSL Pinning is also supported in Starscream. Allow Self-signed certificates: var request = URLRequest(url: URL(string: "ws://localhost:8080/")!)
let pinner = FoundationSecurity(allowSelfSigned: true) // don't validate SSL certificates
let socket = WebSocket(request: request, certPinner: pinner) TODO: Update docs on how to load certificates and public keys into an app bundle, use the builtin pinner and TrustKit. Compression ExtensionsCompression Extensions (RFC 7692) is supported in Starscream. Compression is enabled by default, however compression will only be used if it is supported by the server as well. You may enable compression by adding a var request = URLRequest(url: URL(string: "ws://localhost:8080/")!)
let compression = WSCompression()
let socket = WebSocket(request: request, compressionHandler: compression) Compression should be disabled if your application is transmitting already-compressed, random, or other uncompressable data. Custom QueueA custom queue can be specified when delegate methods are called. By default socket = WebSocket(url: URL(string: "ws://localhost:8080/")!, protocols: ["chat","superchat"])
//create a custom queue
socket.callbackQueue = DispatchQueue(label: "com.vluxe.starscream.myapp") Example ProjectCheck out the SimpleTest project in the examples directory to see how to setup a simple connection to a WebSocket server. RequirementsStarscream works with iOS 8/10.10 or above for CocoaPods/framework support. To use Starscream with a project targeting iOS 7, you must include all Swift files directly in your project. InstallationCocoaPodsCheck out Get Started tab on cocoapods.org. To use Starscream in your project add the following 'Podfile' to your project
Then run:
CarthageCheck out the Carthage docs on how to add a install. The You can install Carthage with Homebrew using the following command: $ brew update
$ brew install carthage To integrate Starscream into your Xcode project using Carthage, specify it in your
AccioCheck out the Accio docs on how to add a install. Add the following to your Package.swift: .package(url: "https://github.com/daltoniam/Starscream.git", .upToNextMajor(from: "4.0.0")), Next, add .target(
name: "App",
dependencies: [
"Starscream",
]
), Then run RogueFirst see the installation docs for how to install Rogue. To install Starscream run the command below in the directory you created the rogue file.
Next open the Swift Package ManagerThe Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the Once you have your Swift package set up, adding Starscream as a dependency is as easy as adding it to the dependencies: [
.Package(url: "https://github.com/daltoniam/Starscream.git", majorVersion: 4)
] OtherSimply grab the framework (either via git submodule or another package manager). Add the Add Copy Frameworks PhaseIf you are running this in an OSX app or on a physical iOS device you will need to make sure you add the TODOs
LicenseStarscream is licensed under the Apache v2 License. ContactDalton CherryAustin Cherry |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论