I'm about to write, but I got stuck on the way ... It's okay to close it without publishing it as a draft ...
The reason why I got stuck is that I understand that WebSocket starts communication from the client side to the server at any time, but I do not know how to communicate from the server side to the client at any time ...
For the time being, the following are preliminary surveys
The mechanism that realizes two-way communication between the browser and the server used to be called polling with JavaScript or comet ... Recently, I don't often see the word Websocket.
For a general talk about two-way communication technology, refer to the following
And the other day I did a client-side implementation of Websocket
What I want to do this time is as the title, and I will implement it myself on both the server side and the client side, so study from 1 ...
After looking at various things, I could understand the following quickly
The above understanding has deepened, if anything, on the client side ...
An instance of WebSocket in JavaScript in the implementation on the browser / HTML + JavaScript side? To describe the behavior / behavior when receiving a message from the server
So, on the server side, an implementation that sends a message when the number of clients increases was described as a sample.
What I want to do is to allow the server side to send a message at any time, and the client side refreshes the screen display when the message is received, and also implements the update process from the client side.
Something a little difficult was written at the beginning, but ... The following also helped me understand how it works.
Docker and uwsgi are a little extra information, but ...
It seems that the implementation on the client side can be used, WebSocket is started when the button is clicked, a message is created from the client side to the server when the connection is started, and when the server side receives the message, the contents are directly sent to the client. To receive a message from the server side and display it at the prompt on the client side
I don't know when the client-side onclose event will occur ...
The program I'm trying to embed this time uses a framework called responder in Python, so the following implementation example will be helpful.
Server side implementation is helpful
@api.route('/ws', websocket=True)
async def websocket(ws):
await ws.accept()
key = ws.headers.get('sec-websocket-key')
clients[key] = ws
try:
while True:
msg = await ws.receive_text()
for client in clients.values():
await client.send_text(msg)
except:
await ws.close()
del clients[key]
The trigger when sending data from the client side to the server is when you press enter, which may be usable
window.onload = function () {
textbox.addEventListener('keypress', function (e) {
//Send a message when the enter key is pressed
if (e.keyCode == 13) {
ws.send(textbox.value);
textbox.value = "";
}
});
}
So, after researching so far, I realized that I didn't even know about simple page drawing with responder in the first place, so I picked up information about that.
It's not just page drawing, but the following is a lot of reference
resp.text
and I was worried for a moment because it was not drawn properly in HTML, but I have to return it with resp.content
... I read and noticedRecommended Posts