-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>WSConnection demo</title> | ||
</head> | ||
<body> | ||
<script> | ||
var ws = new WebSocket("ws://127.0.0.1:9999/"), | ||
messages = document.createElement('ul'); | ||
ws.onmessage = function (event) { | ||
var messages = document.getElementsByTagName('ul')[0], | ||
message = document.createElement('li'), | ||
content = document.createTextNode(event.data); | ||
message.appendChild(content); | ||
messages.appendChild(message); | ||
}; | ||
document.body.appendChild(messages); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import asyncio | ||
|
||
from lemon.app import Lemon | ||
from lemon.wsconnection import WSMessage, WSConnection | ||
|
||
cache = [] | ||
|
||
|
||
async def pull(conn: WSConnection, msg: WSMessage): | ||
print(msg.text) | ||
await conn.send(msg.text) | ||
|
||
|
||
async def push(conns): | ||
while True: | ||
for conn in conns: | ||
await conn.send('xxx') | ||
await asyncio.sleep(0.1) | ||
|
||
|
||
ins = Lemon(debug=True) | ||
|
||
ins.ws(ws_pull=pull, ws_push=push) | ||
|
||
ins.listen() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class WSHeaders(dict): | ||
def __init__(self, raw_headers=None, *args, **kwargs): | ||
super(WSHeaders, self).__init__(*args, **kwargs) | ||
if raw_headers: | ||
for h in raw_headers: | ||
self.__setitem__(h[0].decode(), h[1].decode()) | ||
|
||
def __setitem__(self, key: str, value): | ||
return super(WSHeaders, self).__setitem__(key.lower(), str(value)) | ||
|
||
def __getitem__(self, key: str): | ||
return super(WSHeaders, self).__getitem__(key.lower()) | ||
|
||
def set(self, key: str, value): | ||
return self.__setitem__(key, value) | ||
|
||
|
||
class WSConnection: | ||
def __init__(self, conn): | ||
self.conn = conn | ||
|
||
async def establish(self): | ||
self.conn.accept() | ||
self.conn.listen() | ||
self.conn.connection_open() | ||
|
||
async def destroy(self): | ||
self.conn.reject() | ||
await self.conn.close() | ||
|
||
async def send(self, message_text: str): | ||
return await self.conn.send(message_text) | ||
|
||
|
||
class WSMessage(dict): | ||
def __init__(self, raw_message, **kwargs): | ||
self.path = raw_message['path'] | ||
self.order = raw_message['order'] | ||
self.text = raw_message['text'] | ||
self.bytes = raw_message['bytes'] | ||
|
||
super().__init__(**kwargs) |
Empty file.