Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wspy
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
wspy
Commits
6f65ec01
Commit
6f65ec01
authored
Dec 20, 2014
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More README additions
parent
d4c3c564
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
7 deletions
+22
-7
README.md
README.md
+17
-2
async.py
async.py
+5
-5
No files found.
README.md
View file @
6f65ec01
...
...
@@ -166,7 +166,7 @@ handles client crashes properly. By default, a `Server` instance only logs
every event using Python's
`logging`
module. To create a custom server, The
`Server`
class should be extended and its event handlers overwritten. The event
handlers are named identically to the
`Connection`
event handlers, but they
also receive an additional
`client`
argument. The client argume
tn
is a modified
also receive an additional
`client`
argument. The client argume
nt
is a modified
`Connection`
instance, so you can invoke
`send()`
and
`recv()`
.
For example, the
`EchoConnection`
example above can be rewritten to:
...
...
@@ -187,7 +187,16 @@ For example, the `EchoConnection` example above can be rewritten to:
EchoServer(('', 8000)).run()
The server can be stopped by typing CTRL-C in the command line. The
`KeyboardInterrupt`
raised when this happens is caught by the server.
`KeyboardInterrupt`
raised when this happens is caught by the server, making it
exit gracefully.
The full list of overwritable methods is:
`onopen`
,
`onmessage`
,
`onclose`
,
`onerror`
,
`onping`
,
`onpong`
.
The server uses Python's built-in
[
logging
](
https://docs.python.org/2/library/logging.html
)
module for logging.
Try passing the argument
`loglevel=logging.DEBUG`
to the server constructor if
you are having trouble debugging.
Asynchronous (recommended)
--------------------------
...
...
@@ -199,6 +208,12 @@ sent later when the socket is ready. The client argument is again a modified
`Connection`
instance, with a non-blocking
`send()`
method (
`recv`
is still
blocking, use the server's
`onmessage`
callback instead).
The asynchronous server has one additional method which you can implement:
`AsyncServer.onsent(self, client, message)`
, which is called after a message
has completely been written to the socket. You will probably not need this
unless you are doing something advanced or have to clear a buffer in a
high-performance application.
Extensions
==========
...
...
async.py
View file @
6f65ec01
...
...
@@ -38,7 +38,7 @@ class AsyncConnection(Connection):
for
frame
in
frames
[:
-
1
]:
self
.
sock
.
queue_send
(
frame
)
self
.
sock
.
queue_send
(
frames
[
-
1
],
lambda
:
self
.
onsen
d
(
message
))
self
.
sock
.
queue_send
(
frames
[
-
1
],
lambda
:
self
.
onsen
t
(
message
))
def
send_frame
(
self
,
frame
,
callback
):
self
.
sock
.
queue_send
(
frame
,
callback
)
...
...
@@ -79,7 +79,7 @@ class AsyncConnection(Connection):
self
.
ping_payload
=
payload
self
.
ping_sent
=
True
def
onsen
d
(
self
,
message
):
def
onsen
t
(
self
,
message
):
"""
Called after a message has been written.
"""
...
...
@@ -165,7 +165,7 @@ class AsyncServer(Server):
self
.
epoll
.
modify
(
conn
.
sock
.
fileno
(),
mask
)
def
onsen
d
(
self
,
client
,
message
):
def
onsen
t
(
self
,
client
,
message
):
return
NotImplemented
...
...
@@ -179,9 +179,9 @@ class AsyncClient(Client, AsyncConnection):
AsyncConnection
.
send
(
self
,
message
,
fragment_size
,
mask
)
self
.
server
.
update_mask
(
self
)
def
onsen
d
(
self
,
message
):
def
onsen
t
(
self
,
message
):
logging
.
debug
(
'Finished sending %s to %s'
,
message
,
self
)
self
.
server
.
onsen
d
(
self
,
message
)
self
.
server
.
onsen
t
(
self
,
message
)
if
__name__
==
'__main__'
:
...
...
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