2013-02-04 47 views
3

我用HTML5編寫了一款遊戲。如何只在Google App Engine上提供靜態文件?

python -m SimpleHTTPServer 

然後我打開localhost:8000:本地,如果我運行它纔會起作用。所以,只有一堆.html和.js文件將不起作用。我想把我的遊戲在線,因爲這個Github(頁)是沒有問題的,因爲它不會工作。

這是我需要一個服務器(我也知道localhost:8000/res/不會在App Engine上工作,我需要更改地址)的部分代碼:

var mapFile = new XMLHttpRequest(); 
var self = this; 
mapFile.open("GET", "http://localhost:8000/res/map" + mapNumber.toString() + ".txt", true); 

mapFile.onreadystatechange = function() { 
    if (mapFile.readyState === 4) { 
    if (mapFile.status === 200) { 
     self.lines = mapFile.responseText.split("\n"); 
     self.loadTilesFromLines(); 
    } 
    } 
}; 

mapFile.send(null); 

所以,我聽說Google App Engine可以工作,它支持Python並且非常受歡迎。現在,我不需要像他們有他們的文檔中的任何東西(這是非常精心編寫的):

import webapp2 

class MainPage(webapp2.RequestHandler): 
    def get(self): 
     self.response.headers['Content-Type'] = 'text/plain' 
     self.response.write('Hello, webapp2 World!') 

app = webapp2.WSGIApplication([('/', MainPage)], 
           debug=True) 

所有我需要的是一個SimpleHTTPServer,讓我打開我的index.htmlmy-app.appspot.com

我確實嘗試過這個例子,並啓動並運行,但我無法強制我的瀏覽器打開index.htmlsrc/甚至res/

因此,我甚至不確定Google App Engine是否支持我在此嘗試實現的目標。該文檔只關注構建使用Python的應用程序,而我所需要的所有Python都是SimpleHTTPServer,我認爲我不需要App Engine。

回答

3

是的,這是非常可行的,你想在這裏實現。既然你只是想提供靜態文件,它非常簡單,你不需要包含任何Python代碼。

讓我們假設你有這樣的結構如下:

└── my-game 
    ├── app.yaml 
    └── static 
     ├── index.html 
     ├── js 
     │   └── script.js 
     └── res 
      └── map.txt 

app.yaml應該是這樣的:

application: my-app 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: yes 

handlers: 

- url:/
    static_files: static/index.html 
    upload: static/index.html 

- url:/
    static_dir: static/ 

之後你要安裝Google App Engine SDK(如果你沒做已經),你將能夠從你的終端運行dev_appserver.py命令。如果你有以上的結構嘗試運行它使用下列內容:

$ dev_appserver.py /path/to/my-game 

如果一切順利,你就可以看到你的index.htmlhttp://localhost:8080,在map.txthttp://localhost:8080/res/map.txt,你應該能夠找出休息。

請注意,您仍然可以使用static目錄中的python -m SimpleHTTPServer運行您的應用程序,並在localhost:8000上測試它。

+0

非常感謝您的回答(寫得很好,非常清晰),它在localhost上完美運行。我現在只需要使用my-app.appspot.com地址進行測試。 –

+0

@ user996056這是有史以來最快的接受答案:)它應該在部署時正常工作..但如果你有任何問題只是在這裏平。 – Lipis

+0

'2013-02-04 13:51:37,608錯誤appcfg.py:2203處理文件''時發生錯誤:HTTP錯誤400:錯誤的請求。中止。 錯誤400:---開始服務器輸出--- 壞業主名單(400) 所有者列表invalid.' @Lipis,這只是開始發生後,我改變了我的app.yaml到你。這是更新時得到的錯誤。它曾經工作得很好。 –

相關問題