https://qiita.com/shun_sakamoto/items/7944d0ac4d30edf91fde
venvで手軽にPythonの仮想環境を構築しよう
以下のエラー対策は仮想環境しかない!
pip3 install asyncio error: externally-managed-environment
× This environment is externally managed
. env/bin/activate ./env/bin/pip3 install asyncio で成功!
---------------------------------------------
https://qiita.com/youtoy/items/f74e4ae26a1b67c310c0 :: simple websocket echo server
--------------------echo server----------
import asyncio
import websockets
async def echo(websocket, path):
print("クライアントが接続しました")
try:
async for message in websocket:
print(f"受信: {message}")
response = f"Echo: {message}"
await websocket.send(response)
print(f"送信: {response}")
except websockets.exceptions.ConnectionClosed:
print("クライアントとの接続が閉じられました")
async def main():
# localhost の 8765 番ポートでサーバーを起動
async with websockets.serve(echo, "localhost", 8765):
print("WebSocketサーバーが起動しました")
await asyncio.Future() # 永久に待機
if __name__ == '__main__':
asyncio.run(main())
-----------------------------------------------------
https://qiita.com/youtoy/items/ae1dd6ee36ac35c6e519 より
-----------------------client.py------------------------------------
import asyncio
from websockets.asyncio.client import connect
async def hello():
async with connect("ws://localhost:8765") as websocket:
await websocket.send("Hello world!")
message = await websocket.recv()
print(message)
if __name__ == "__main__":
asyncio.run(hello())
https://qiita.com/youtoy/items/ae1dd6ee36ac35c6e519 はブロードキャスサーバもあり
ーーーーーーーーー
https://qiita.com/hikichi_shoto/items/f976970a114bab20325a flaskでチャット、有益
192.168.11.8:5002でアクセスしてチャットができた!
------------------------------------------------------------
Node.jsでWebSocketサーバーを実装するには、wsのようなライブラリを使用します。このライブラリを使うと、クライアントの接続を待ち受け、接続されたクライアントと双方向でメッセージの送受信ができます。
サーバー側の実装(Node.js)
- ライブラリのインストール:
まず、
wsライブラリをインストールします。- サーバーの作成と起動:
以下のコードを
server.jsなどのファイルに保存します。クライアント側の実装(JavaScript)
ブラウザの開発者ツールや、HTMLとJavaScriptでクライアントを作成します。
0 件のコメント:
コメントを投稿