~starplant/panda3d-docs/trunk

« back to all changes in this revision

Viewing changes to Downloading_a_File.txt

  • Committer: starplant at gmail
  • Date: 2008-06-16 07:18:45 UTC
  • Revision ID: starplant@gmail.com-20080616071845-i5o4v9nx1wkfkfh6
start

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Downloading_a_File
 
2
==================
 
3
 
 
4
To download a file while the game is running without blocking the connections one has to use HTTPClient and HTTPChannel objects. This will allow the file to be downloaded in the background using the downloadTask task.
 
5
self.http = HTTPClient()
 
6
self.channel = self.http.makeChannel(True)
 
7
self.channel.beginGetDocument(DocumentSpec('http://my.url/'))
 
8
self.rf = Ramfile()
 
9
self.channel.downloadToRam(self.rf)
 
10
taskMgr.add(self.downloadTask, 'download')
 
11
def downloadTask(self, task):
 
12
if self.channel.run():
 
13
# Still waiting for file to finish downloading.
 
14
return task.cont
 
15
if not self.channel.isDownloadComplete():
 
16
print "Error downloading file."
 
17
return task.done
 
18
data = self.rf.getData()
 
19
print "got data:"
 
20
print data
 
21
return task.done
 
22
You can also download to file
 
23
channel.downloadToFile(Filename(fileName))
 
24
The file channel can be quarried for further information while the game is running to get the current download state.
 
25
mbDownloaded = self.channel.getBytesDownloaded()/1024/1024
 
26
percentDownloaded = 100.*self.channel.getBytesDownloaded()/channel.getFileSize()
 
27
   Downloading_a_File ==================
 
28
 
 
29
   To download a file while the game is running without blocking the
 
30
   connections one has to use HTTPClient and HTTPChannel objects. This
 
31
   will allow the file to be downloaded in the background using the
 
32
   downloadTask task.
 
33
self.http = HTTPClient()
 
34
self.channel = self.http.makeChannel(True)
 
35
self.channel.beginGetDocument(DocumentSpec('http://my.url/'))
 
36
self.rf = Ramfile()
 
37
self.channel.downloadToRam(self.rf)
 
38
taskMgr.add(self.downloadTask, 'download')
 
39
 
 
40
def downloadTask(self, task):
 
41
    if self.channel.run():
 
42
        # Still waiting for file to finish downloading.
 
43
        return task.cont
 
44
    if not self.channel.isDownloadComplete():
 
45
        print "Error downloading file."
 
46
        return task.done
 
47
    data = self.rf.getData()
 
48
    print "got data:"
 
49
    print data
 
50
    return task.done 
 
51
 
 
52
   You can also download to file
 
53
channel.downloadToFile(Filename(fileName))
 
54
 
 
55
   The file channel can be quarried for further information while the
 
56
   game is running to get the current download state.
 
57
mbDownloaded = self.channel.getBytesDownloaded()/1024/1024
 
58
percentDownloaded = 100.*self.channel.getBytesDownloaded()/channel.getFileSize(
 
59
)
 
60
 
 
61
   To download a file while the game is running without blocking the
 
62
   connections one has to use HTTPClient and HTTPChannel objects. This
 
63
   will allow the file to be downloaded in the background using the
 
64
   downloadTask task.
 
65
self.http = HTTPClient()
 
66
self.channel = self.http.makeChannel(True)
 
67
self.channel.beginGetDocument(DocumentSpec('http://my.url/'))
 
68
self.rf = Ramfile()
 
69
self.channel.downloadToRam(self.rf)
 
70
taskMgr.add(self.downloadTask, 'download')
 
71
 
 
72
def downloadTask(self, task):
 
73
    if self.channel.run():
 
74
        # Still waiting for file to finish downloading.
 
75
        return task.cont
 
76
    if not self.channel.isDownloadComplete():
 
77
        print "Error downloading file."
 
78
        return task.done
 
79
    data = self.rf.getData()
 
80
    print "got data:"
 
81
    print data
 
82
    return task.done 
 
83
 
 
84
   You can also download to file
 
85
channel.downloadToFile(Filename(fileName))
 
86
 
 
87
   The file channel can be quarried for further information while the
 
88
   game is running to get the current download state.
 
89
mbDownloaded = self.channel.getBytesDownloaded()/1024/1024
 
90
percentDownloaded = 100.*self.channel.getBytesDownloaded()/channel.getFileSize(
 
91
)