- 前提:每則訊息都帶有一個可排序的唯一標識(例如 messageID 或「遞增的序號」),或使用「時間戳 (timestamp) + 伺服器的邏輯排序」來判斷訊息先後。
- local 端儲存:在 local 端中,為每個房間記錄一個 lastReceivedMessageID(或 lastReceivedTimestamp),並在每次成功收到新的訊息後都更新。
- 當用戶重新連線時,前端將 lastReceivedMessageID 傳給後端,後端依此回傳從這個 ID(或 Timestamp)之後的新訊息列表。
- 當偵測到斷線(WebSocket 連線斷開事件),客戶端開始進行重連邏輯。
- 在連線成功的初始握手或授權階段,客戶端告知伺服器「我在哪些聊天室,有哪些 lastReceivedMessageID」。。
- 伺服器回傳遺漏訊息:
- 從用戶提供的 lastReceivedMessageID 之後開始,批次或分頁 (pagination) 地將訊息傳回。
- 為避免伺服器壓力,後端可設定一次最多回傳多少筆訊息,若訊息量過多則分多次回傳,讓客戶端透過多次請求或多次 WebSocket Event 接收。
- 客戶端合併訊息:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// 測速網域的物件 | |
class DomainSpeedTester { | |
// 用來儲存測試結果的內部資料結構 | |
private var results: [(domain: String, time: Double)] = [] | |
// 請求圖片的 function,圖片不需儲存,最後回傳下載時間 (ms) | |
func downloadImg(domain: String) async throws -> Double { | |
let urlString = "https://\(domain)/test-img" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func isOneEditDistance(_ s: String, _ t: String) -> Bool { | |
if (s.count == t.count) { | |
return countOfReplaced(s, t) == 1 | |
} | |
if (s.count == t.count - 1) { | |
return isRemoved(s, t) | |
} | |
if (t.count == s.count - 1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# In[ ]: | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
get_ipython().magic('matplotlib inline') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 這段程式碼來自莫煩 Python: https://morvanzhou.github.io/tutorials/machine-learning/keras/2-1-regressor/ | |
from keras.models import Sequential | |
from keras.layers import Dense | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# 建立 X, Y 兩組資料用來練習 keras 的使用 | |
X = np.linspace(-1, 1, 200) | |
np.random.shuffle(X) | |
Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200, )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from peewee import * | |
import datetime | |
class DateHandler: | |
def __init__(self): | |
pass | |
THIS_YEAR = datetime.datetime.now().year |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import StringIO | |
import csv | |
import requests | |
from bs4 import BeautifulSoup | |
import time | |
import random | |
from Models import * | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/', methods=['GET']) | |
def verify(): | |
# when the endpoint is registered as a webhook, it must echo back | |
# the 'hub.challenge' value it receives in the query arguments | |
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"): | |
if not request.args.get("hub.verify_token") == os.environ["VERIFY_TOKEN"]: | |
return "Verification token mismatch", 403 | |
return request.args["hub.challenge"], 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def send_message(recipient_id, message_text): | |
params = { | |
"access_token": os.environ["PAGE_ACCESS_TOKEN"] | |
} | |
headers = { | |
"Content-Type": "application/json" | |
} | |
data = json.dumps({ | |
"recipient": { | |
"id": recipient_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"entry": [ | |
{ | |
"id": "entry_id", | |
"time": 1485670019761, | |
"messaging": [ | |
{ | |
"sender": { | |
"id": "USER_ID" | |
}, |
NewerOlder