gpt4 book ai didi

ios - GCDWebServer中不同本地资源的两个路径

转载 作者:行者123 更新时间:2023-11-30 10:51:53 24 4
gpt4 key购买 nike

我需要为不同的本地资源(HTML5 游戏)初始化具有两个路径的 GCDWebServer。

现在我可以使用 addGETHandler 仅为一个资源创建路径。该处理程序只能使用一次,如果您再次使用它,旧的处理程序将被删除,新的处理程序将取代它。

这是我的代码:

let firstGameFolderPath = Bundle.main.path(forResource: "game1", ofType: nil)
ServerService.gcdWebServer.addGETHandler(forBasePath: "/", directoryPath: firstGameFolderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
ServerService.gcdWebServer.start(withPort: 8080, bonjourName: "GCD Web Server")

如果有人有任何想法如何解决这个任务,那就太好了。

附注我有想法创建两台具有不同端口的服务器,但是太贵了。

所有发生的 Controller (方法 didSelectRowAt):

import UIKit

class MenuViewController: UITableViewController, UINavigationControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
self.navigationController?.delegate = self
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let menuCell = tableView.dequeueReusableCell(withIdentifier: "MenuCell") as? MenuTableViewCell
switch indexPath.row {
case 0:
menuCell?.updateCell(title: "Главная")
case 1:
menuCell?.updateCell(title: "Первая игра")
case 2:
menuCell?.updateCell(title: "Вторая игра")
default:
break
}
return menuCell!
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let menuCell = tableView.cellForRow(at: indexPath) as! MenuTableViewCell

switch indexPath.row {
case 0:
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
case 1:
menuCell.activityIndicator.startAnimating()
let firstGameFolderPath = Bundle.main.path(forResource: "game1", ofType: nil)
ServerService.gcdWebServer.addGETHandler(forBasePath: "/", directoryPath: firstGameFolderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
ServerService.gcdWebServer.start(withPort: 8080, bonjourName: "GCD Web Server")
self.perform(#selector(showGameVC), with: nil, afterDelay: 1) //Delay for launching WebServer
case 2:
menuCell.activityIndicator.startAnimating()
let secondGameFolderPath = Bundle.main.path(forResource: "game2", ofType: nil)
ServerService.gcdWebServer.addGETHandler(forBasePath: "/", directoryPath: secondGameFolderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
ServerService.gcdWebServer.start(withPort: 8080, bonjourName: "GCD Web Server")
self.perform(#selector(showGameVC), with: nil, afterDelay: 1) //Delay for launching WebServer
default:
break
}

tableView.deselectRow(at: indexPath, animated: true)
}

@objc func showGameVC() {
let gameViewController = self.storyboard?.instantiateViewController(withIdentifier: "GameVC") as! GameViewController
self.navigationController?.pushViewController(gameViewController, animated: true)
}
}

Link to repo

最佳答案

您可以向 GCDWebServer 添加任意数量的处理程序,因此只需添加另一个具有不同目录路径和不同基本路径的处理程序即可:

ServerService.gcdWebServer.addGETHandler(forBasePath: "/assets1/", directoryPath: firstGameFolderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
ServerService.gcdWebServer.addGETHandler(forBasePath: "/assets2/", directoryPath: secondGameFolderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)

This handler can be used once if you will use it again, the old handler will be removed, and the new one will take its place.

事实并非如此,除非您调用 -removeAllHandlers,否则处理程序永远不会被删除 - 请参阅 https://github.com/swisspol/GCDWebServer/blob/master/GCDWebServer/Core/GCDWebServer.m .

关于ios - GCDWebServer中不同本地资源的两个路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54377751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com