I am using daphne
in Django 4.1 for a basic simple slow reply from the server.
class ExampleWebSocket(AsyncWebsocketConsumer):
async def receive(self, text_data):
print("Received : " + text_data)
if (text_data == "START"):
await self.send_message(self)
async def send_message(self, res):
for j in range(0, 10):
time.sleep(1) # Time it takes to retrieve some data
await self.send(text_data=f"Data received as: {j}")
await self.send(text_data="DONE")
Everything’s working, but my console.log is showing 1 through 10 after 10 seconds in one go and not as Data received as: n
after each second.
const contactServer = () =>
{
let socket = new WebSocket('ws://localhost:8989/test');
socket.addEventListener('open', function (event)
{
socket.send('START');
});
socket.addEventListener('message', function (event)
{
console.log(event.data);
if (event.data == "DONE")
{
console.log('DONE socket !');
// socket.close();
}
});
}