利用websocket實現聊天小應用

引言

原文連接html

趁着悠閒研究了一下websocket,並利用python和html簡單的實現了簡單的聊天系統,
但願對想要了解的有所幫助,有什麼好的意見還請你們提出。

Overview

  • 編程語言:python,htmlpython

  • 實現功能: 在瀏覽器開啓兩個對話窗口並進行聊天web

  • 須要的第三方庫:編程

    • pip install websocketsjson

  • 關於websocke:後端

    • WebSocket protocol 是HTML5一種新的協議瀏覽器

    • 實現了瀏覽器與服務器全雙工通訊服務器

  • 我爲何要用:websocket

    • 實現web頁面的即時通信socket

    • 去除輪詢帶來的諸多缺點(這個網上一搜一大把)

關於流程圖

  • 發信過程
    fx.jpg

後端代碼

# codind=utf-8
import asyncio
import websockets
import json
import time
ALLSOKETS = {}


def add_connection(ws_id, ws):
    global ALLSOKETS
    ALLSOKETS[ws_id] = ws
    print(ALLSOKETS)


def filter_handle(ws_id):
    return ALLSOKETS.get(ws_id)


async def handler(websocket, path):
    # print(path)
    while True:
.
.
.

原文連接

其餘代碼請看原文連接

http://yunsonbai.top/2016/12/18/%E5%88%A9%E7%94%A8websocket%E5%AE%9E%E7%8E%B0%E8%81%8A%E5%A4%A9%E5%B0%8F%E5%BA%94%E7%94%A8/

或點擊

原文連接

相關文章
相關標籤/搜索