[英]How can I use SocketRocket in a Swift Project?
I'm new in iOS programming and still learning Swift. 我是iOS编程的新手,仍然在学习Swift。 I'm trying to use SocketRocket library https://github.com/facebook/SocketRocket in a Swift Project in order to connect to my Web Server, created using MAMP, through Web Sockets.
我试图在Swift项目中使用SocketRocket库https://github.com/facebook/SocketRocket ,以便通过Web套接字连接到使用MAMP创建的Web服务器。 I know that there is Starscream https://github.com/daltoniam/Starscream ready to use in Swift but it gives errors and I cannot make it work as I explained here: How to fix "websocket is disconnected: Invalid HTTP upgrade" error using Starscream
我知道有Starscream https://github.com/daltoniam/Starscream准备在Swift中使用,但它给出了错误,并且无法按我在此处的说明进行操作: 如何解决“ websocket已断开连接:无效的HTTP升级”错误使用红蜘蛛
Rocket Socket is written in Objective-C and I don't understand it, tried to look the documentation but I don't know how to translate it into Swift and implementing its methods in my project. Rocket Socket是用Objective-C编写的,我不理解它,试图查看文档,但是我不知道如何将其转换为Swift并在我的项目中实现其方法。 Already installed SocketRocket using Cocoapods.
已经使用Cocoapods安装了SocketRocket。 So it doesn't need the header-bridging file because I used 'use_frameworks!'
因此它不需要标题桥接文件,因为我使用了“ use_frameworks!”。 in the podfile.
在podfile中。
ViewController.swift file: ViewController.swift文件:
import UIKit import SocketRocket 导入UIKit导入SocketRocket
class ViewController: UIViewController, SRWebSocketDelegate { //ERROR: Type 'ViewController' does not conform to protocol 'SRWebSocketDelegate' Do you want to add protocol stubs? class ViewController:UIViewController,SRWebSocketDelegate {//错误:类型'ViewController'不符合协议'SRWebSocketDelegate'是否要添加协议存根?
var urlRequest = NSURLRequest(URL: NSURL(string: "http://host.com")) //EROOR: Cannot convert value of type 'NSURL?' to expected argument type 'URL' Insert ' as! URL'
var socket = SRWebSocket(URLRequest: urlRequest)
//let socket = SRWebSocket(url: "ws://localhost:8888")
override func viewDidLoad() {
super.viewDidLoad()
socket.open()
socket.send()
socket.close()
}
func webSocketDidOpen(webSocket: SRWebSocket!) {
print("socket opened");
}
func webSocket(webSocket: SRWebSocket!, didCloseWithCode code: Int, reason: String!, wasClean: Bool) {
print("code: \(code) reason:\(reason) ");
}
func webSocket(webSocket: SRWebSocket!, didFailWithError error: NSError!) {
print("error: \(error)");
}
func webSocket(webSocket: SRWebSocket!, didReceiveMessage message: AnyObject!) {
print("received message")
}
} }
This is the code I tried to write... But there are errors as shown in the comments. 这是我尝试编写的代码...但是有错误,如注释中所示。 Hope you guys could help me.
希望你们能帮助我。 Thanks!!!
谢谢!!! :)
:)
Hi Cristian, You can use " pod 'Socket.IO-Client-Swift' " also which uses Starscream. 嗨,克里斯蒂安,您可以使用也使用Starscream的“ pod'Socket.IO-Client-Swift'”。
For documentation you can look here also. 有关文档,您也可以在这里查看。 Socket.IO-Client-Swift
Socket.IO-客户端-Swift
private var socket: SocketIOClient?
private var manager: SocketManager?
private func initializeSocket() {
self.manager = SocketManager(socketURL: URL(string: self.baseUrlForSocket)!, config: [.log(true), .forceNew(true), .reconnectAttempts(10), .reconnectWait(6000), .connectParams(["authorization": authKey]), .forceWebsockets(true), .compress])
self.socket = manager?.defaultSocket
}
Here baseUrlForSocket is the socket url which you have been provided and authKey is the authorization value. 这里baseUrlForSocket是您提供的套接字URL,而authKey是授权值。
In case of any concern, please let me know. 如有任何疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.