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
bd6d116d
Commit
bd6d116d
authored
Jun 30, 2013
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added JSONMessage and some comments + code cleanup
parent
a6952f5b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
9 deletions
+47
-9
__init__.py
__init__.py
+9
-0
message.py
message.py
+11
-1
server.py
server.py
+23
-4
websocket.py
websocket.py
+4
-4
No files found.
__init__.py
View file @
bd6d116d
from
websocket
import
websocket
from
server
import
Server
from
frame
import
Frame
,
ControlFrame
from
Connection
import
Connection
from
message
import
Message
,
TextMesage
,
BinaryMessage
,
JSONMessage
__all__
=
[
'websocket'
,
'Server'
,
'Frame'
,
'ControlFrame'
,
'Connection'
,
'Message'
,
'TextMessage'
,
'BinaryMessage'
,
'JSONMessage'
]
message.py
View file @
bd6d116d
import
json
from
frame
import
Frame
,
OPCODE_TEXT
,
OPCODE_BINARY
from
frame
import
Frame
,
OPCODE_TEXT
,
OPCODE_BINARY
__all__
=
[
'Message'
,
'TextMessage'
,
'BinaryMessage'
]
__all__
=
[
'Message'
,
'TextMessage'
,
'BinaryMessage'
,
'JSONMessage'
]
class
Message
(
object
):
class
Message
(
object
):
...
@@ -30,6 +32,14 @@ class BinaryMessage(Message):
...
@@ -30,6 +32,14 @@ class BinaryMessage(Message):
super
(
BinaryMessage
,
self
).
__init__
(
OPCODE_BINARY
,
payload
)
super
(
BinaryMessage
,
self
).
__init__
(
OPCODE_BINARY
,
payload
)
class
JSONMessage
(
TextMessage
):
def
__init__
(
self
,
dictionary
,
**
kwargs
):
self
.
data
=
{}
self
.
data
.
extend
(
dictionary
)
self
.
data
.
extend
(
kwargs
)
super
(
JSONMessage
,
self
).
__init__
(
json
.
dumps
(
self
.
data
))
OPCODE_CLASS_MAP
=
{
OPCODE_CLASS_MAP
=
{
OPCODE_TEXT
:
TextMessage
,
OPCODE_TEXT
:
TextMessage
,
OPCODE_BINARY
:
BinaryMessage
,
OPCODE_BINARY
:
BinaryMessage
,
...
...
server.py
View file @
bd6d116d
...
@@ -10,6 +10,25 @@ from exceptions import HandshakeError
...
@@ -10,6 +10,25 @@ from exceptions import HandshakeError
class
Server
(
object
):
class
Server
(
object
):
"""
Websocket server object, used to manage multiple client connections.
Example usage:
>>> import websocket
>>> class GameServer(websocket.Server):
>>> def onopen(self, client):
>>> # client connected
>>> def onclose(self, client):
>>> # client disconnected
>>> def onmessage(self, client, message):
>>> # handle message from client
>>> GameServer(port=8000).run()
"""
def
__init__
(
self
,
port
,
hostname
=
''
,
loglevel
=
logging
.
INFO
,
protocols
=
[]):
def
__init__
(
self
,
port
,
hostname
=
''
,
loglevel
=
logging
.
INFO
,
protocols
=
[]):
logging
.
basicConfig
(
level
=
loglevel
,
logging
.
basicConfig
(
level
=
loglevel
,
format
=
'%(asctime)s: %(levelname)s: %(message)s'
,
format
=
'%(asctime)s: %(levelname)s: %(message)s'
,
...
@@ -55,16 +74,16 @@ class Server(object):
...
@@ -55,16 +74,16 @@ class Server(object):
self
.
onclose
(
client
,
code
,
reason
)
self
.
onclose
(
client
,
code
,
reason
)
def
onopen
(
self
,
client
):
def
onopen
(
self
,
client
):
logging
.
debug
(
'Opened socket to %s'
%
client
)
logging
.
debug
(
'Opened socket to %s'
,
client
)
def
onmessage
(
self
,
client
,
message
):
def
onmessage
(
self
,
client
,
message
):
logging
.
debug
(
'Received %s from %s'
%
(
message
,
client
)
)
logging
.
debug
(
'Received %s from %s'
,
message
,
client
)
def
onping
(
self
,
client
,
payload
):
def
onping
(
self
,
client
,
payload
):
logging
.
debug
(
'Sent ping "%s" to %s'
%
(
payload
,
client
)
)
logging
.
debug
(
'Sent ping "%s" to %s'
,
payload
,
client
)
def
onpong
(
self
,
client
,
payload
):
def
onpong
(
self
,
client
,
payload
):
logging
.
debug
(
'Received pong "%s" from %s'
%
(
payload
,
client
)
)
logging
.
debug
(
'Received pong "%s" from %s'
,
payload
,
client
)
def
onclose
(
self
,
client
,
code
,
reason
):
def
onclose
(
self
,
client
,
code
,
reason
):
msg
=
'Closed socket to %s'
%
client
msg
=
'Closed socket to %s'
%
client
...
...
websocket.py
View file @
bd6d116d
...
@@ -60,10 +60,10 @@ class websocket(object):
...
@@ -60,10 +60,10 @@ class websocket(object):
client handshake). Note that the handshake may raise an HandshakeError
client handshake). Note that the handshake may raise an HandshakeError
exception.
exception.
"""
"""
client
,
address
=
self
.
sock
.
accept
()
sock
,
address
=
self
.
sock
.
accept
()
client
=
websocket
(
client
)
wsock
=
websocket
(
sock
)
client
.
server_handshake
()
wsock
.
server_handshake
()
return
client
,
address
return
wsock
,
address
def
connect
(
self
,
address
):
def
connect
(
self
,
address
):
"""
"""
...
...
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