Last active
December 30, 2020 06:15
-
-
Save shamsimam/ba041f1d3f6a53917794d658e835d908 to your computer and use it in GitHub Desktop.
socketio websocket example on Waiter
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
| Server initialized for aiohttp. | |
| INFO:Server initialized for aiohttp. | |
| DEBUG:Using selector: KqueueSelector | |
| INFO:127.0.0.6 [30/Dec/2020:04:48:40 +0000] "GET / HTTP/1.1" 200 851 "-" "waiter-syncer/3386a8c" | |
| INFO:127.0.0.6 [30/Dec/2020:04:48:45 +0000] "GET / HTTP/1.1" 200 851 "-" "waiter-shell" | |
| INFO:127.0.0.6 [30/Dec/2020:04:48:46 +0000] "GET / HTTP/1.1" 200 851 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" | |
| INFO:127.0.0.6 [30/Dec/2020:04:48:51 +0000] "GET / HTTP/1.1" 200 851 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" | |
| aiqFh5dpDGGicomvAAAA: Sending packet OPEN data {'sid': 'aiqFh5dpDGGicomvAAAA', 'upgrades': [], 'pingTimeout': 5000, 'pingInterval': 25000} | |
| INFO:aiqFh5dpDGGicomvAAAA: Sending packet OPEN data {'sid': 'aiqFh5dpDGGicomvAAAA', 'upgrades': [], 'pingTimeout': 5000, 'pingInterval': 25000} | |
| INFO:aiqFh5dpDGGicomvAAAA: Received request to upgrade to websocket | |
| INFO:aiqFh5dpDGGicomvAAAA: Upgrade to websocket successful | |
| INFO:aiqFh5dpDGGicomvAAAA: Received packet MESSAGE data 0 | |
| INFO:aiqFh5dpDGGicomvAAAA: Sending packet MESSAGE data 0{"sid":"W4Nh7JmV93cnN_CLAAAB"} | |
| INFO:127.0.0.6 [30/Dec/2020:04:48:55 +0000] "GET / HTTP/1.1" 200 851 "-" "waiter-shell" | |
| INFO:127.0.0.6 [30/Dec/2020:04:49:05 +0000] "GET / HTTP/1.1" 200 851 "-" "waiter-shell" | |
| INFO:aiqFh5dpDGGicomvAAAA: Received packet MESSAGE data 2["message","msg-1609303753700"] | |
| INFO:received event "message" from W4Nh7JmV93cnN_CLAAAB [/] | |
| INFO:emitting event "message" to all [/] | |
| INFO:aiqFh5dpDGGicomvAAAA: Sending packet MESSAGE data 2["message","0073573039061-gsm"] | |
| INFO:127.0.0.6 [30/Dec/2020:04:49:15 +0000] "GET / HTTP/1.1" 200 851 "-" "waiter-shell" | |
| INFO:aiqFh5dpDGGicomvAAAA: Sending packet PING data None | |
| INFO:aiqFh5dpDGGicomvAAAA: Received packet PONG data | |
| INFO:aiqFh5dpDGGicomvAAAA: Received packet MESSAGE data 2["message","msg-1609303758654"] | |
| INFO:received event "message" from W4Nh7JmV93cnN_CLAAAB [/] | |
| INFO:emitting event "message" to all [/] | |
| INFO:aiqFh5dpDGGicomvAAAA: Sending packet MESSAGE data 2["message","4568573039061-gsm"] | |
| INFO:aiqFh5dpDGGicomvAAAA: Received packet MESSAGE data 2["message","msg-1609303759488"] | |
| INFO:received event "message" from W4Nh7JmV93cnN_CLAAAB [/] | |
| INFO:emitting event "message" to all [/] | |
| INFO:aiqFh5dpDGGicomvAAAA: Sending packet MESSAGE data 2["message","8849573039061-gsm"] | |
| INFO:aiqFh5dpDGGicomvAAAA: Received packet MESSAGE data 2["message","msg-1609303760169"] | |
| ... |
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
| #!/usr/bin/env python3 | |
| from aiohttp import web | |
| import logging | |
| import os | |
| import socketio | |
| logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) | |
| ## creates a new Async Socket IO Server | |
| sio = socketio.AsyncServer(logger=True, engineio_logger=True, cors_allowed_origins=[]) | |
| ## Creates a new Aiohttp Web Application | |
| app = web.Application() | |
| # Binds our Socket.IO server to our Web App | |
| ## instance | |
| sio.attach(app) | |
| ## we can define aiohttp endpoints just as we normally | |
| ## would with no change | |
| async def index(request): | |
| response_text = """<!-- index.html --> | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"/> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"/> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"/> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <button onClick="sendMsg()">Hit Me</button> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js"></script> | |
| <script> | |
| const socket = io("http://socketio.localtest.me:9091", {transports: ["websocket"]}); | |
| function sendMsg() { | |
| socket.emit("message", "msg-" + Date.now()); | |
| } | |
| socket.on("message", function(data) { | |
| console.log(data); | |
| }); | |
| </script> | |
| </body> | |
| </html>""" | |
| return web.Response(text=response_text, content_type='text/html') | |
| ## If we wanted to create a new websocket endpoint, | |
| ## use this decorator, passing in the name of the | |
| ## event we wish to listen out for | |
| @sio.on('message') | |
| async def print_message(sid, message): | |
| ## When we receive a new event of type | |
| ## 'message' through a socket.io connection | |
| ## we print the socket ID and the message | |
| print("Socket ID: " , sid) | |
| print(message) | |
| ## await a successful emit of our reversed message | |
| ## back to the client | |
| await sio.emit('message', message[::-1]) | |
| ## We bind our aiohttp endpoint to our app | |
| ## router | |
| app.router.add_get('/', index) | |
| ## We kick off our server | |
| port_str = os.environ.get('PORT0', '8090') | |
| host_str = os.environ.get('HOST', '0.0.0.0') | |
| if __name__ == '__main__': | |
| web.run_app(app, host=host_str, port=int(port_str)) |
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
| { | |
| "cmd": "/path/to/socketio/server.py", | |
| "cmd-type": "shell", | |
| "concurrency-level": 100, | |
| "cpus": 0.1, | |
| "health-check-url": "/", | |
| "max-instances": 1, | |
| "mem": 256, | |
| "run-as-user": "*", | |
| "version": "local", | |
| "token": "socketio.localtest.me" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment