Janus Websocket Connection Issue

Hello. My time is very limited so I hope you look at the topic immediately.
I am having problems with Janus websocket connection.
I installed my Janus and TURN server in a virtual machine I created on Google Cloud.
I did the TURN configurations correctly.
Everything looks correct on the Janus side; janus.jcfg, janus.transport.websocket.jcfg… I also created firewall rules for the ports, but unfortunately I can’t connect to ws from an external device. I have a simple python project to do this:
main.py:

# main.py

import asyncio
from signaling.janus_signaling import JanusSignaler

async def main():
    signaler = JanusSignaler("ws://a.b.c.d:8188/janus")
    await signaler.connect()

asyncio.run(main())

janus_signaling.py:

# signaling/janus_signaling.py

import asyncio
import websockets
import json
import uuid

class JanusSignaler:
    def __init__(self, ws_url):
        self.ws_url = ws_url
        self.ws = None

    async def connect(self):
        print("WebSocket bağlantısı kuruluyor")
        self.ws = await websockets.connect(self.ws_url)
        print("Buraya geldik mi hocam")
        #Bağlandıktan sonra Janus'a create isteği gönder
        transaction = str(uuid.uuid4())
        create_msg = {
            "janus": "create",
            "transaction": transaction
        }
    
        await self.ws.send(json.dumps(create_msg))
        print("Create mesajı gönderildi")
    
        #Cevabı al
        response = await self.ws.recv()
        print("Gelen cevap: ")
        print(json.dumps(json.loads(response), indent=4))

When I run these codes, I get the following error:


 Traceback (most recent call last):
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/http.py", line 120, in read_response
    status_line = await read_line(stream)
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 134, in read_http_response
    status_code, reason, headers = await read_response(self.reader)
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/http.py", line 122, in read_response
    raise EOFError("connection closed while reading HTTP status line") from exc
EOFError: connection closed while reading HTTP status line
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "main.py", line 10, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "main.py", line 8, in main
    await signaler.connect()
  File "/home/nida/raspberry_client/signaling/janus_signaling.py", line 15, in connect
    self.ws = await websockets.connect(self.ws_url)
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 656, in __await_impl__
    await protocol.handshake(
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 316, in handshake
    status_code, response_headers = await self.read_http_response()
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 136, in read_http_response
    raise InvalidMessage("did not receive a valid HTTP response") from exc
websockets.legacy.exceptions.InvalidMessage: did not receive a valid HTTP response

When I send a websocat request to Janus from the terminal, I get the following error:

~$ websocat ws://35.242.205.185:8188/janus
websocat: WebSocketError: WebSocket protocol error
websocat: error running

When I run this same command in VM with localhost or 127.0.0.0.1, I get the same error.

The Janus service works fine, and if I run it, the logs come as follows:

Traceback (most recent call last):
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/http.py", line 120, in read_response
    status_line = await read_line(stream)
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 134, in read_http_response
    status_code, reason, headers = await read_response(self.reader)
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/http.py", line 122, in read_response
    raise EOFError("connection closed while reading HTTP status line") from exc
EOFError: connection closed while reading HTTP status line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main.py", line 10, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "main.py", line 8, in main
    await signaler.connect()
  File "/home/nida/raspberry_client/signaling/janus_signaling.py", line 15, in connect
    self.ws = await websockets.connect(self.ws_url)
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 656, in __await_impl__
    await protocol.handshake(
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 316, in handshake
    status_code, response_headers = await self.read_http_response()
  File "/home/nida/.local/lib/python3.8/site-packages/websockets/legacy/client.py", line 136, in read_http_response
    raise InvalidMessage("did not receive a valid HTTP response") from exc
websockets.legacy.exceptions.InvalidMessage: did not receive a valid HTTP response

I would be really happy if you could help me solve this problem. Have a good day.

Is it correct - 127.0.0.0.1 ?

yes, I misspelled it here

Did you try “ws://a.b.c.d:8188”, without “/janus” ?

yes, i tried also with “…:8188/”
and every command exited with same error

i think errors are cause of websocket, everything is correct like static ip in turn configuration, ports and ips in janus configurations etc. but because of a reason somethings going wrong and i cant solve it since 2 days

Don’t forget the subprotocol. You may want to check the documentation, before working on any code: RESTful, WebSockets, RabbitMQ, MQTT, Nanomsg and UnixSockets API

2 Likes

You can’t imagine how happy i am! Thanks a lot for your quick answer, may lord protect you :slight_smile: