Created
May 19, 2024 07:12
-
-
Save sefgit/00abf901c80386848144f8acef14221a to your computer and use it in GitHub Desktop.
socketio on tornado
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width,initial-scale=1.0"> | |
<title>Socket.IO chat</title> | |
<style> | |
#messages { list-style-type: none; margin: 0; padding: 0; } | |
#messages > li { padding: 0.5rem 1rem; } | |
#messages > li:nth-child(odd) { background: #efefef; } | |
</style> | |
</head> | |
<body> | |
<ul id="messages"></ul> | |
<form id="form" action=""> | |
<input id="input" autocomplete="off" /><button>Send</button> | |
</form> | |
<script src="socket.io.min.js"></script> | |
<script> | |
let counter = 0; | |
const socket = io({ | |
auth: { | |
serverOffset: 0 | |
}, | |
ackTimeout: 10000, | |
retries: 3, | |
}); | |
const form = document.getElementById('form'); | |
const input = document.getElementById('input'); | |
const messages = document.getElementById('messages'); | |
form.addEventListener('submit', (e) => { | |
e.preventDefault(); | |
if (input.value) { | |
const clientOffset = `${socket.id}-${counter++}`; | |
socket.emit('chat message', input.value, clientOffset); | |
input.value = ''; | |
} | |
}); | |
socket.on('chat message', (msg, serverOffset) => { | |
const item = document.createElement('li'); | |
item.textContent = msg; | |
messages.appendChild(item); | |
window.scrollTo(0, document.body.scrollHeight); | |
socket.auth.serverOffset = serverOffset; | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
import asyncio | |
from os import path | |
from tornado.ioloop import IOLoop | |
from tornado.web import Application, RequestHandler, StaticFileHandler | |
from socketio import AsyncServer, get_tornado_handler | |
from tornado.options import define, options, parse_command_line | |
filepath = path.abspath(__file__) | |
base_folder = path.dirname(filepath) | |
define("port", type=int, default=8000, help="port to bind to") | |
define("cert", type=str, default=path.join(base_folder, "localhost.pem"), help="path to cert file") | |
define("key", type=str, default=path.join(base_folder, "localhost-key.pem"), help="path to key file") | |
options.parse_command_line() | |
sio = AsyncServer( | |
async_mode='tornado', | |
cors_allowed_origins='*' # !!! IMPORTANT !!! | |
) | |
@sio.on('chat message') | |
async def chat_event(sid, data, auth): | |
print('chat message ', sid, data, auth) | |
await sio.emit('chat message', data[::-1], sid); | |
return "OK", sid | |
handlers = [ | |
(r"/socket.io/", get_tornado_handler(sio)), | |
(r"/res/(.*)", StaticFileHandler, {"path": path.join(base_folder, "res")}), | |
] | |
app = Application(handlers) | |
app.listen( | |
options.port, | |
ssl_options = {"certfile": options.cert, "keyfile": options.key} | |
) | |
IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment