3

I am wrote a basic Websocket client-server code while learning it. The server program is in Python and Client program is in Javascript. The server side is sending messages but client isn't receiving. Can you guys tell what's wrong?

Javascript Code:

<script type="text/javascript">
    let socket = new WebSocket("ws://localhost:8765");

    socket.onconnect = function(event){
        console.log('Connected.');
    }

    socket.onmesssage = function(event)
    {
        console.log("Message received, " + event.data);
        var a = document.getElementById("p1");
        a.innerHTML = "You are client no.:" + event.data;
    }
</script>

Python Code:

import asyncio, websockets
i=0

async def start(websocket, path):
    global i
    i += 1
    await websocket.send(str(i))
    print("Number of clients: " + str(i));

print('Started...')
start_server = websockets.serve(start, "localhost", 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

The Javascript code doesn't work at all but Python code works(it print's statements after sending message).

1 Answer 1

1

There is a typo by the way.

It should be socket.onmessage NOT onmesssage There are 3 s in your code. Change to..

socket.onmessage = function(event)
    {
        console.log("Message received, " + event.data);
        var a = document.getElementById("p1");
        a.innerHTML = "You are client no.:" + event.data;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.