mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-19 00:41:41 +00:00
web socket using tornado
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,3 +1,9 @@
|
||||
Python/__pycache__/WebSiteHandler\.cpython-37\.pyc
|
||||
|
||||
\.idea/
|
||||
|
||||
*.pyc
|
||||
|
||||
Python/app/__pycache__/
|
||||
|
||||
Python/app/handlers/__pycache__/
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import http.server, socketserver, os
|
||||
import websockets, asyncio
|
||||
import multiprocessing
|
||||
|
||||
HttpServerPort = 80
|
||||
SocketServerPort = 8765
|
||||
|
||||
|
||||
def run_server():
|
||||
web_dir = os.path.join(os.path.dirname(__file__), '../Site')
|
||||
os.chdir(web_dir)
|
||||
handler = http.server.SimpleHTTPRequestHandler
|
||||
with socketserver.TCPServer(("", HttpServerPort), handler) as httpd:
|
||||
print('server has started')
|
||||
httpd.serve_forever()
|
||||
|
||||
|
||||
async def web_socket_handler(socket, path):
|
||||
print('test')
|
||||
data = await socket.recv()
|
||||
print(data)
|
||||
|
||||
|
||||
def test():
|
||||
print('socket started')
|
||||
socket = websockets.serve(web_socket_handler, 'ws://localhost', SocketServerPort)
|
||||
print(socket)
|
||||
|
||||
|
||||
def run_all():
|
||||
html_process = multiprocessing.Process(target=run_server)
|
||||
socket_process = multiprocessing.Process(target=test)
|
||||
html_process.start()
|
||||
socket_process.start()
|
||||
|
||||
@@ -3,6 +3,7 @@ import tornado.websocket
|
||||
import os
|
||||
|
||||
from handlers.MainHandler import MainHandler
|
||||
from handlers.SocketHandler import ChameleonWebSocket
|
||||
from tornado.options import define
|
||||
|
||||
define("port", default=8888, help="run on the given port", type=int)
|
||||
@@ -11,6 +12,7 @@ define("port", default=8888, help="run on the given port", type=int)
|
||||
class ChameleonApplication(tornado.web.Application):
|
||||
def __init__(self):
|
||||
handlers = [(r"/", MainHandler),
|
||||
(r"/websocket", ChameleonWebSocket),
|
||||
(r"/CSS/(.*)", tornado.web.StaticFileHandler, {'path': os.path.join(os.path.dirname(__file__), "../../Site/CSS")}),
|
||||
(r"/JS/(.*)", tornado.web.StaticFileHandler,
|
||||
{'path': os.path.join(os.path.dirname(__file__), "../../Site/JS")})]
|
||||
|
||||
Binary file not shown.
Binary file not shown.
12
Python/app/handlers/SocketHandler.py
Normal file
12
Python/app/handlers/SocketHandler.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import tornado.websocket
|
||||
|
||||
class ChameleonWebSocket(tornado.websocket.WebSocketHandler):
|
||||
def open(self):
|
||||
print("WebSocke opend")
|
||||
|
||||
def on_message(self, message):
|
||||
print(message)
|
||||
self.write_message("why the fuck did you send a messege")
|
||||
def on_close(self):
|
||||
print("websocket closed")
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -15,5 +15,14 @@
|
||||
<a class="nav-link" href="#">3D</a>
|
||||
</nav>
|
||||
</div>
|
||||
<script>
|
||||
var ws = new WebSocket("ws://localhost:8888/websocket");
|
||||
ws.onopen = function () {
|
||||
ws.send("hello");
|
||||
}
|
||||
ws.onmessage = function (ev) {
|
||||
alert(ev.data);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user