1 from mod_pywebsocket
import common
2 from mod_pywebsocket
import handshake
3 from mod_pywebsocket
import stream
4 from mod_pywebsocket
import msgutil
7 def web_socket_do_extra_handshake(request
):
11 def web_socket_transfer_data(request
):
12 # pywebsocket does not mask message by default. We need to build a frame manually to mask it.
13 request
.connection
.write(stream
.create_text_frame('First message', mask
=False))
15 request
.connection
.write(stream
.create_text_frame('Fragmented ', opcode
=common
.OPCODE_TEXT
, fin
=0, mask
=False))
16 request
.connection
.write(stream
.create_text_frame('message', opcode
=common
.OPCODE_CONTINUATION
, fin
=1, mask
=False))
18 request
.connection
.write(stream
.create_text_frame('', mask
=False))
20 msgutil
.send_message(request
, 'END')
22 # Wait for the client to start closing handshake.
23 # To receive a close frame, we must use an internal method of request.ws_stream.
24 opcode
, payload
, final
, reserved1
, reserved2
, reserved3
= request
.ws_stream
._receive
_frame
()
25 assert opcode
== common
.OPCODE_CLOSE
31 # Send a masked close frame. Clients should be able to handle this frame and
32 # the WebSocket object should be closed cleanly.
33 request
.connection
.write(stream
.create_close_frame('', mask
=False))
35 # Prevents pywebsocket from starting its own closing handshake.
36 raise handshake
.AbortedByUserException('Abort the connection')