The WebSocket protocol is a key component of the modern web, providing a way for browsers to communicate with servers in real time. It’s also an important part of many games and other interactive apps. This library aims to provide a fully conforming implementation of the WebSocket protocol, as well as some convenience methods for working with it.
The starscream ios is a library that conforms to the WebSocket (RFC 6455) specification. It allows developers to create WebSocket clients and servers in Swift for iOS and OSX.
In Swift, Starscream is a WebSocket (RFC 6455) compliant library.
- All of the Autobahn test suite’s requirements are met.
- Nonblocking. Thanks to GCD, everything occurs in the background.
- Support for TLS/WSS.
- Extensions for compression are supported (RFC 7692)
Download and install the framework.
The framework must first be imported. For information on how to add the framework to your project, see the Installation instructions.
Establish a connection to the WebSocket Server.
You may connect to your WebSocket server after it’s been imported. It’s worth noting that socket is generally better used as a property so that it isn’t deallocated immediately after being created.
URLRequest(url: URL(string: “http://localhost:8080”)!) var request = URLRequest(url: URL(string: “http://localhost:8080”)!) var request = URLRequest(url: URL request. timeoutInterval = 5; timeoutInterval = 5; timeoutInterval = WebSocket = socket (request: request) delegate = self socket; socket.delegate = self socket; socket.delegate = self socket; socket connect()
After you’ve connected, you may use either a delegate or a closure to handle WebSocket events.
Getting information from a WebSocket
didReceive collects all WebSocket events into a single, manageable enum.
func was successful. switch event case.connected(let headers): is receive(event: WebSocketEvent, client: WebSocket) True print = connected scenario when “websocket is connected: (headers)” is disjointed(let cause, let code): False print = connected (case) “websocket is disconnected: (reason) with code: (code)” print(“Received text: (string)”) case text(let string) case binary(let data): print(“Data received: (data.count)”) ping(_): this is a break case. pong(_): case of a break. viability Break case has been changed(_). reconnect Break case is suggested(_). cancelled: case isConnected = false isConnected = false handle error(let error) errno(errno) errno(errno) errno
The following would be the conclusion:
socket.onEvent = event in switch event / handle events in the same manner as before…
Using a WebSocket to send data
assemble a binary frame
The writeData function makes sending Data (binary) data to the server easy.
socket. write(data: data) /use the socket to write some data!
construct a string frame
The writeString function is similar to writeData, except it transmits text/string instead of data.
/example of how to write text over the socket: socket.write(string: “Hello Server!”)
construct a ping frame
The writePing method is similar to write, except that it sends a ping control frame instead.
/example of how to write a ping control frame across the socket using socket.write(ping: Data())!
make a ping pong frame
The writePong method is similar to writePing, except it transmits a pong control frame instead of a ping control frame.
/example of how to write a pong control frame across the socket using socket.write(pong: Data())
You won’t have to manually send pongs since Starscream will react to receiving ping control frames.
If you need to manage this process for any reason, you may disable respondToPingWithPong and turn off the automated ping response.
socket.respondToPingWithPong = false;/ Pings should not be automatically responded to with pongs.
You won’t need to do this in most instances.
The disconnect method does exactly what it says on the tin: it shuts the socket.
If required, the disconnect function may additionally transmit a custom close code.
socket.disconnect (closeCode.normal.rawValue) (closeCode.normal.rawValue) (closeCode.normal.rawValue) (close
Timeouts, Protocols, and Custom Headers
You may change the default websocket headers, add your own, and provide a timeout:
URLRequest(url: URL(string: “ws:/localhost:8080/”) request var request = URLRequest(url: URL(string: “ws:/localhost:8080/”) request var request = URLRequest(url: URL(string: ” timeoutInterval = 5;/ Sets the connection request’s timeout. request with setValue(“other protocols”, forHTTPHeaderField: “Sec-WebSocket-Protocol”) request with setValue(“14”, forHTTPHeaderField: “Sec-WebSocket-Version”) request with setValue(“chat,superchat”, forHTTPHeaderField: “Sec-WebSocket-Protocol”) let socket = WebSocket setValue(“Everything is Awesome!”, forHTTPHeaderField: “My-Awesome-Header”) (request: request)
Starscream also has SSL Pinning support.
Self-signed certificates are permitted:
URLRequest(url: URL(string: “ws:/localhost:8080/”)!) var request = URLRequest(url: URL(string: “ws:/localhost:8080/”)!) var request = URLRequest(url: URL let pinner = FoundationSecurity to be true (allowSelfSigned: true) / SSL certificates aren’t validated. let socket = WebSocket(request: request, certificate: certificate) pinner, pinner, pinner, pinner, pinner, pinner, pin
TODO: Update the documentation on how to utilize the built-in pinner and TrustKit to load certificates and public keys into an app bundle.
Extensions for Compression
Starscream supports Compression Extensions (RFC 7692). Compression is enabled by default, however it will only be utilized if the server also supports compression. Compression may be enabled by adding a compressionHandler:
URLRequest(url: URL(string: “ws:/localhost:8080/”)!) var request = URLRequest(url: URL(string: “ws:/localhost:8080/”)!) var request = URLRequest(url: URL WS = let compression Compression() let socket = WebSocket(request: request; compression: compression); Compression (handler)
If your application is sending already-compressed, random, or other uncompressable data, compression should be disabled.
When delegate methods are invoked, a custom queue may be provided. By default, DispatchQueue.main is utilized, which means that all delegate method calls are handled by the main thread. It’s worth noting that all WebSocket processing takes place in the background; just the delegate method calls are altered when the queue is modified. The actual processing is always done in the background and does not cause your program to stop.
WebSocket(url: URL(string: “ws:/localhost:8080/”)! socket = WebSocket(url: URL(string: “ws:/localhost:8080/”)! [“chat”,”superchat”])))))))))))))))))))))))))) Create a custom queue socket using this code. DispatchQueue = callbackQueue (label: “com.vluxe.starscream.myapp”)
To learn how to set up a basic connection to a WebSocket server, look at the SimpleTest project in the examples directory.
For CocoaPods/framework compatibility, Starscream requires iOS 8/10.10 or later. To utilize Starscream with an iOS 7 project, you must include all Swift files in your project directly.
On cocoapods.org, go to the Get Started page.
Add the following ‘Podfile’ to your project to utilize Starscream in it.
source ‘https://github.com/CocoaPods/Specs.git’ platform :ios, 4.0.0′ pod ‘Starscream’, ‘> 4.0.0’, ‘9.0’ use frameworks!
How to add a Carthage install may be found in the Carthage documentation. Shared schemes are already set up in the Starscream architecture.
Use the following command to install Carthage using Homebrew:
$ brew install carthage $ brew update
To use Carthage to incorporate Starscream into your Xcode project, add the following to your Cartfile:
>= 4.0.0 “> github “daltoniam/Starscream” >= 4.0.0 “> github “daltoniam/Starscream”
For further information about how to add an install, see the Accio documentation.
To your Package.swift file, add the following:
.upToNextMajor(from: “4.0.0”),.package(url: “https://github.com/daltoniam/Starscream.git”),.package(url: “https://github.com/daltoniam/Starscream.git”),.package(url: “https://github.com/daltoniam/Starscream
Then, as follows, add Starscream to your App targets dependencies:
.objective ( name: “App”, dependencies: [ “Starscream”, ] ),
After that, run accio update.
For further information on how to install Rogue, see to the installation instructions.
Run the command below in the location where you saved the rogue file to install Starscream.
https://github.com/daltoniam/Starscream rogue add
After that, enter the libs folder and drag the Starscream.xcodeproj file into your Xcode project. After that, add the Starscream.framework to your “Link Binary with Libraries” step in your “Build Phases.” Make sure the libs folder is included in your.gitignore file.
Swift Package Manager is a program that allows you to manage your
The Swift Package Manager, which is built into the Swift compiler, is a tool for automating the distribution of Swift code.
Once you’ve built up your Swift package, adding Starscream as a dependent is as simple as adding it to the Package’s dependencies field. swift.
dependencies: [.Package(url: “https://github.com/daltoniam/Starscream.git”, majorVersion: 4) ] [.Package(url: “https://github.com/daltoniam/Starscream.git”, majorVersion: 4) ]
Simply take the framework and use it (either via git submodule or another package manager).
In your Xcode project, add the Starscream.xcodeproj file. After that, add the Starscream.framework to your “Link Binary with Libraries” step in your “Build Phases.”
Phase of Adding Copy Frameworks
If you’re using this in an OSX app or on a real iOS device, you’ll need to include the Starscream.framework in your app package. To do so, open the target configuration window in Xcode by clicking on the blue project icon and choosing the application target from the sidebar’s “Targets” header. Open the “Build Phases” panel from the tab bar at the top of that window. Add Starscream.framework to the “Link Binary with Libraries” group. Select “New Copy Files Phase” from the Plus button at the top left of the screen. Change the name of this new phase to “Copy Frameworks,” change the “Destination” to “Frameworks,” and add Starscream.framework.
The Apache v2 License applies to Starscream.
Austin Cherry is a writer who lives in Austin,
The starscream swift example is a conforming WebSocket (RFC 6455) library in Swift for iOS and OSX.
- best swift websocket library
- starscream swiftui
- websocket ios
- websocket viabilitychanged