Building42 / Telegraph

Secure Web Server for iOS, tvOS and macOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cant access to server from local

siempay opened this issue · comments

Hi,

I did install Telegraph by pod, and then in application: didFinishLaunchingWithOptions. I have:

let server = Server()
try! server.start(onInterface: "localhost", port: 9000)
		
server.route(.get, "status") { () -> HTTPResponse in
   return HTTPResponse(.ok, content: "server is running")
}

server.serveBundle(.main, "/")

but I dont know how to access localhost:9000 from my browser or simulator browser.

What I really need is: to have an app that work as a server and an other app that send request and show data, so I'm gonna need to call my server from outside. how can I do that ?

I should of declared var server: Server!  as a singleton on at least outside the controller

@siempay Yes, the problem is that your Server instance is automatically deallocated at the end of the function. You can retain it in an instance variable if you want to prevent that from happening.

For example:

import UIKit
import Telegraph

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  var server: Server!

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    server = Server()
    try! server.start(port: 9000)

    server.route(.GET, "status") { () -> HTTPResponse in
      return HTTPResponse(.ok, content: "server is running")
    }

    server.serveBundle(.main, "/")

    return true
  }
}

I recommend using the latest version of Telegraph (0.20).