
    [i                         d Z ddlZddlmZmZmZmZmZ ddlm	Z	m
Z
 ddlmZ  e       Zej                  d       edd	      fd
edefd       Zy)u  
WebSocket 端點(階段 3c)。

連線方式(前端 JS):
    const ws = new WebSocket(`ws://localhost:8000/api/ws?token=${access_token}`);
    ws.onmessage = (e) => { ... };

server → client 事件型別:
    {type: "hello", user_id, online_count}                — 連線成功歡迎
    {type: "message_new", channel_id, message}            — 新訊息
    {type: "message_updated", channel_id, message}        — 訊息被編輯
    {type: "message_deleted", channel_id, message_id}     — 訊息被刪
    {type: "reaction_changed", channel_id, message}       — 反應變動(完整 message 為新狀態)
    {type: "read_receipt", channel_id, user_id, last_read_at}
                                                           — 有人讀到此刻
    {type: "channel_new", channel}                        — 新增頻道
    {type: "channel_updated", channel}                    — 頻道資訊變動
    {type: "channel_deleted", channel_id}                 — 頻道被刪
    {type: "ping"}                                        — 伺服器 keepalive

client → server 訊息(目前都不需要,只接收):
    {type: "ping"} → 回 {type: "pong"}
    N)	APIRouterQuery	WebSocketWebSocketDisconnectstatus)
TokenErrordecode_token)managerz/api/ws.u7   access token,跟 HTTP Authorization header 用同一把)description	websockettokenc                   K   	 t        |d      }| j                          d{    t        j                  | |       d{    	 | j                  dt        |      t        j                         d       d{    	 	 t        j                  | j                         d	       d{   }|rE	 d
dl}|j                  |      }|j!                  d      dk(  r| j                  ddi       d{    v# t        $ r- | j                  t        j                  d       d{  7   Y yw xY w7 7 7 7 7 G# t"        $ r Y Pw xY w# t        j$                  $ r1 	 | j                  ddi       d{  7   n# t"        $ r Y Y n
w xY wY w xY wn# t&        $ r Y nw xY wt        j(                  | |       y# t        j(                  | |       w xY ww)u^   主要的 WebSocket 入口。

    Token 過期 / 無效 → close(1008 policy violation)
    access)expected_typezinvalid token)codereasonNhello)typeuser_idonline_countg      N@)timeoutr   r   pingpong)r	   r   closer   WS_1008_POLICY_VIOLATIONacceptr
   connect	send_jsonstrconnection_countasynciowait_forreceive_textjsonloadsget	ExceptionTimeoutErrorr   
disconnect)r   r   r   datar$   msgs         ./volume1/homes/robertsu/coba/app/app/api/ws.pywebsocket_endpointr-   $   s    uH= 



//)W
---/!!7|#446#
  	 	 $--i.D.D.FPTUU#"jj.776?f4"+"5"5vv6F"GGG #  oo6#B#B?o[[[
 -	 V H$ '' #--vv.>???   @
   	9g.9g.s3  GC7 GD0GD3G3F ;D5<F (E
 *D7+E
 2?D; 1D92D; 6F 7-D-$D'%D-*G,D--G3G5F 7E
 9D; ;	EE
 EE
 
FE;4E75E;:F;	FFF FFF FF F8 	FF8 FF8 !G8GG)__doc__r!   fastapir   r   r   r   r   app.core.securityr   r	   app.core.ws_managerr
   routerr   r   r-        r,   <module>r5      s`   0  L L 6 ' 
 ) s(ab2/2/2/ 2/r4   