Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
f324d13e
Commit
f324d13e
authored
Sep 28, 2009
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
webchat: use class instead of global variables
parent
18754f7e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
39 deletions
+45
-39
examples/webchat/chat/views.py
examples/webchat/chat/views.py
+45
-39
No files found.
examples/webchat/chat/views.py
View file @
f324d13e
...
@@ -6,50 +6,56 @@ from django.http import HttpResponse
...
@@ -6,50 +6,56 @@ from django.http import HttpResponse
from
gevent.event
import
Event
from
gevent.event
import
Event
from
webchat
import
settings
from
webchat
import
settings
messages
=
[]
MESSAGES_SIZE
=
200
new_message_event
=
Event
()
class
ChatRoom
(
object
):
def
new_message
(
from_
,
body
):
cache_size
=
200
def
__init__
(
self
):
self
.
cache
=
[]
self
.
new_message_event
=
Event
()
def
main
(
self
,
request
):
if
self
.
cache
:
request
.
session
[
'cursor'
]
=
self
.
cache
[
-
1
][
'id'
]
return
render_to_response
(
'index.html'
,
{
'MEDIA_URL'
:
settings
.
MEDIA_URL
,
'messages'
:
self
.
cache
})
def
message_new
(
self
,
request
):
name
=
request
.
META
.
get
(
'REMOTE_ADDR'
)
or
'Anonymous'
msg
=
create_message
(
name
,
request
.
POST
[
'body'
])
self
.
cache
.
append
(
msg
)
if
len
(
self
.
cache
)
>
self
.
cache_size
:
self
.
cache
=
self
.
cache
[
-
self
.
cache_size
:]
self
.
new_message_event
.
set
()
self
.
new_message_event
.
clear
()
return
json_response
(
msg
)
def
message_updates
(
self
,
request
):
cursor
=
request
.
session
.
get
(
'cursor'
)
if
not
self
.
cache
or
cursor
==
self
.
cache
[
-
1
][
'id'
]:
self
.
new_message_event
.
wait
()
assert
cursor
!=
self
.
cache
[
-
1
][
'id'
],
cursor
try
:
for
index
,
m
in
enumerate
(
self
.
cache
):
if
m
[
'id'
]
==
cursor
:
return
json_response
({
'messages'
:
self
.
cache
[
index
+
1
:]})
return
json_response
({
'messages'
:
self
.
cache
})
finally
:
if
self
.
cache
:
request
.
session
[
'cursor'
]
=
self
.
cache
[
-
1
][
'id'
]
else
:
request
.
session
.
pop
(
'cursor'
,
None
)
room
=
ChatRoom
()
main
=
room
.
main
message_new
=
room
.
message_new
message_updates
=
room
.
message_updates
def
create_message
(
from_
,
body
):
data
=
{
'id'
:
str
(
uuid
.
uuid4
()),
'from'
:
from_
,
'body'
:
body
}
data
=
{
'id'
:
str
(
uuid
.
uuid4
()),
'from'
:
from_
,
'body'
:
body
}
data
[
'html'
]
=
render_to_string
(
'message.html'
,
dictionary
=
{
'message'
:
data
})
data
[
'html'
]
=
render_to_string
(
'message.html'
,
dictionary
=
{
'message'
:
data
})
return
data
return
data
def
last_message_id
():
if
messages
:
return
messages
[
-
1
][
'id'
]
def
main
(
request
):
if
messages
:
request
.
session
[
'cursor'
]
=
messages
[
-
1
][
'id'
]
return
render_to_response
(
'index.html'
,
{
'MEDIA_URL'
:
settings
.
MEDIA_URL
,
'messages'
:
messages
})
def
message_new
(
request
):
name
=
request
.
META
.
get
(
'REMOTE_ADDR'
)
or
'Anonymous'
msg
=
new_message
(
name
,
request
.
POST
[
'body'
])
messages
.
append
(
msg
)
if
len
(
messages
)
>
MESSAGES_SIZE
:
del
messages
[
0
]
new_message_event
.
set
()
new_message_event
.
clear
()
return
json_response
(
msg
)
def
message_updates
(
request
):
cursor
=
request
.
session
.
get
(
'cursor'
)
if
cursor
==
last_message_id
():
new_message_event
.
wait
()
assert
cursor
!=
last_message_id
(),
cursor
try
:
for
index
,
m
in
enumerate
(
messages
):
if
m
[
'id'
]
==
cursor
:
return
json_response
({
'messages'
:
messages
[
index
+
1
:]})
return
json_response
({
'messages'
:
messages
})
finally
:
if
messages
:
request
.
session
[
'cursor'
]
=
messages
[
-
1
][
'id'
]
else
:
request
.
session
.
pop
(
'cursor'
,
None
)
def
json_response
(
value
,
**
kwargs
):
def
json_response
(
value
,
**
kwargs
):
kwargs
.
setdefault
(
'content_type'
,
'text/javascript; charset=UTF-8'
)
kwargs
.
setdefault
(
'content_type'
,
'text/javascript; charset=UTF-8'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment