我需要使用两条路径针对不同的本地资源(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")
如果有人对如何解决此任务有任何想法,那将是很好的。
P.S。 我本来想创建2个具有不同端口的服务器,但这太昂贵了。
发生一切的控制器(方法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)
}
}
答案 0 :(得分:0)
您可以尝试给它们提供不同的基本路径。
let firstGameFolderPath = Bundle.main.path(forResource: "game1", ofType: nil)
ServerService.gcdWebServer.addGETHandler(forBasePath: "/game1", directoryPath: firstGameFolderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
ServerService.gcdWebServer.start(withPort: 8080, bonjourName: "GCD Web Server")
let secondGameFolderPath = Bundle.main.path(forResource: "game2", ofType: nil)
ServerService.gcdWebServer.addGETHandler(forBasePath: "/game2", directoryPath: secondGameFolderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
ServerService.gcdWebServer.start(withPort: 8080, bonjourName: "GCD Web Server")
,然后当您需要加载游戏时,可以将相同的game1或game2添加到您的帖子请求网址中。
像这样
let game1Request = URLRequest(url: URL(string:"{YourGCDWebServerURL}/game1")!)
let game2Request = URLRequest(url: URL(string:"{YourGCDWebServerURL}/game2")!)
答案 1 :(得分:0)
您可以向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)
如果再次使用此处理程序,则该处理程序只能使用一次,旧的处理程序将被删除,而新的处理程序将取代它。
事实并非如此,除非调用-removeAllHandlers
-参见https://github.com/swisspol/GCDWebServer/blob/master/GCDWebServer/Core/GCDWebServer.m,否则永远不会删除处理程序。