简体   繁体   English

如何在Swift项目中使用SocketRocket?

[英]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.

相关问题 通过Pods将SocketRocket添加到Swift项目 - Adding SocketRocket to a Swift project through Pods 如何在socket.io中使用SocketRocket? - How to use SocketRocket with socket.io? 我如何在iOS Swift的单个项目中使用两个GMSMapViews - How can i use two GMSMapViews in single project in iOS Swift 我可以在Swift 3项目中使用Swift 2.3框架吗? - Can I use Swift 2.3 frameworks in a Swift 3 project? SWIFT:如何在Swift中使用subscriberCellularProviderDidUpdateNotifier - SWIFT: How I can use subscriberCellularProviderDidUpdateNotifier with Swift SocketRocket尝试安装socketrocket后我可以让PonyDebugger工作,我得到3个Apple Match-O链接器错误 - SocketRocket After trying to install socketrocket so that I can make PonyDebugger work, I'm getting 3 Apple Match-O linker errors 如何将iOS / Swift项目转换为可以使用实时取景的状态? - How can I transform my iOS/Swift project to a state where I will be able to use live views? 如何在混合Xcode项目(Objective-C和Swift)中使用XCGLogger - How can I use XCGLogger in a mixed Xcode project (Objective-C and Swift) 如何在Swift项目的文件夹中使用现有的DB.sqlite? - How can i use existing DB.sqlite from folder in swift project? 如何将这个Swift 3 Singleton用作我的IOS项目的全局计时器? - How can I use this Swift 3 Singleton as a Global Timer for my IOS project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM