<!DOCTYPE html> <meta charset="utf-8"/> <title>WebSocket Test</title> <script language="javascript" type="text/javascript"> var wsUri = "ws://127.0.0.1:9999"; var output; function init() { output = document.getElementById("output"); testWebSocket(); } function testWebSocket() { var websocket = new WebSocket(wsUri); console.log('Ready State: ' + websocket.readyState) websocket.onopen = function (evt) { onOpen(evt) }; websocket.onclose = function (evt) { onClose(evt) }; websocket.onmessage = function (evt) { onMessage(evt) }; websocket.onerror = function (evt) { onError(evt) }; } function onOpen(evt) { writeToScreen("CONNECTED"); } function onClose(evt) { writeToScreen("DISCONNECTED"); } function onMessage(evt) { writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>'); } function onError(evt) { writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); } function writeToScreen(message) { var pre = document.createElement("p"); pre.style.wordWrap = "break-word"; pre.innerHTML = message; output.appendChild(pre); } window.addEventListener("load", init, false); </script> <h2>WebSocket Test</h2> <div id="output"></div>
и серверная часть
import serial import sys sys.path.append('../..') import time from SingleSocket.Output import Output SOCKET_PORT = 9999 SOCKET_HOST = '127.0.0.1' stream = Output(host=SOCKET_HOST, port=SOCKET_PORT, web=True) port = stream.start() print('Emitting port: {:d}'.format(port)) if not stream.running: exit(0) ser = serial.Serial("/dev/ttyS0", baudrate=2400) def starts(): while True: ser.write(b"Q1\r") ups1= ser.read(47) stream.append_msg(ups1) try: starts() except Exception: starts()
в самом низу программы я перехватываю исключение если бесперебойник дисконектится, но при повоторном запуске программы получаю ты же самую ошибку, на всякий случай выкладываю все логи при запуске серверной части
serrrggggeee@serrrggggeee-Arbyte-Quint-B-M4D3A-H61 ~/socket $ python websocket2.py [*] Listening on 127.0.0.1:9999 [*] Accepted from: 127.0.0.1:38626 [*] Received: GET / HTTP/1.1 Host: 127.0.0.1:9999 Connection: Upgrade Pragma: no-cache Cache-Control: no-cache Upgrade: websocket Origin: file/ Sec-WebSocket-Version: 13 User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8,ru;q=0.6,de;q=0.4,uk;q=0.2 Cookie: django_language=ru; csrftoken=HU9xTxpwGwyXcK06qF71EghChCgprIQM Sec-WebSocket-Key: P+WrSSz4Nz5pWbd83pnxHg== Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits [*] Websocket protocol is enabled Origin : file/ Upgrade : websocket Accept-Language : en-US,en;q=0.8,ru;q=0.6,de;q=0.4,uk;q=0.2 Accept-Encoding : gzip, deflate, sdch get : GET / HTTP/1.1 Sec-WebSocket-Version : 13 Connection : Upgrade Sec-WebSocket-Key : P+WrSSz4Nz5pWbd83pnxHg== User-Agent : Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36 Host : 127.0.0.1:9999 code : Cookie : django_language=ru; csrftoken=HU9xTxpwGwyXcK06qF71EghChCgprIQM Pragma : no-cache Cache-Control : no-cache Sec-WebSocket-Extensions : permessage-deflate; client_max_window_bits [*] Handshake response: HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: 8DkOY/GmBaiV5E5gGiyp5cdQUjU= Emitting port: 9999 [*] Message sent: (227.5 227.0 227.5 039 50.0 13.8 37.8 00001000 Traceback (most recent call last): File "websocket2.py", line 31, in <module> starts() File "websocket2.py", line 25, in starts ups1= ser.read(47) File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 460, in read raise SerialException('device reports readiness to read but returned no data (device disconnected?)') serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)