Im having some problem when joining Electron and Python using web sockets (mostly as a learning experience):
Build a desktop app using Electron Ok
Build a Python program that monitors some things Ok
Connect Python with Electron using socket.io Not working
First I tested socket.io in python with the 'latency' example in https://github.com/miguelgrinberg/python-socketio/tree/master/examples/server/aiohttp. It worked great when I took a look at http://localhost:8080/ in Chrome.
The next step was to ask Electron to load the same url in the desktop app with mainWindow.loadURL('http://localhost:8080')
(I used the template from the official electron-quick-start
example). I just had to add two lines into the html as shown in here: Electron: jQuery is not defined for jquery to load correctly in Electron, but it again worked nicely.
Now I tried to create an index.html
with the same content as latency.html
and load it with mainWindow.loadFile('gui/index.html')
. I changed the line var socket = io.connect();
for var socket = io.connect('ws://localhost:8080');
(I did also tried ws://127.0.0.1:8080
and ws://192.168.<x>.<x>:8080
). The html file loads correctly but the socket begins to connect and disconnect repeatedly with a Bad Request error (I added two console.log lines):
(console.log) connected
(error) POST http://localhost:8080/socket.io/?EIO=3&transport=polling&t=MosNuF3&sid=c62ce5a6090c4b72bf3f7c6916da6ce7 400 (Bad Request) **polling-xhr.js:264**
(console.log) disconnected
(warning) websocket.js:235 WebSocket connection to 'ws://localhost:8080/socket.io/?EIO=3&transport=websocket&sid=c62ce5a6090c4b72bf3f7c6916da6ce7' failed: WebSocket is closed before the connection is established.
(error) POST http://localhost:8080/socket.io/?EIO=3&transport=polling&t=MosNuFS&sid=c62ce5a6090c4b72bf3f7c6916da6ce7 400 (Bad Request) **polling-xhr.js:264**
(console.log) connected
etc...
(console.log) disconnected
etc...
On the server side there is also an error when I stop the Electron app:
Unhandled exception
Traceback (most recent call last):
File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_protocol.py", line 447, in start
await resp.prepare(request)
File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_response.py", line 353, in prepare
return await self._start(request)
File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_response.py", line 667, in _start
return await super()._start(request)
File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_response.py", line 410, in _start
await writer.write_headers(status_line, headers)
File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\http_writer.py", line 112, in write_headers
self._write(buf)
File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\http_writer.py", line 67, in _write
raise ConnectionResetError('Cannot write to closing transport')
ConnectionResetError: Cannot write to closing transport
What does this Bad Request error mean in this context ? How would the connection work correctly ?
Thanks.