皆さんこんにちは、
パート 1 では、シンプルなサーバー側(cè)時(shí)計(jì)を作成しました https://dev.to/blinkinglight/golang-data-star-1o53/
そして今度は、https://nats.io と https://data-star.dev を使用して、より複雑なものを書くことにしました -
書き込んだ內(nèi)容を返すチャットボット:
いくつかの Golang コード:
package handlers import ( "encoding/json" "fmt" "log" "net/http" "github.com/blinkinglight/chat-data-star/web/views/chatview" "github.com/delaneyj/datastar" "github.com/delaneyj/toolbelt" "github.com/delaneyj/toolbelt/embeddednats" "github.com/go-chi/chi/v5" "github.com/gorilla/sessions" "github.com/nats-io/nats.go" ) func SetupChat(router chi.Router, session sessions.Store, ns *embeddednats.Server) error { type Message struct { Message string `json:"message"` Sender string `json:"sender"` } nc, err := ns.Client() if err != nil { return err } nc.Subscribe("chat.>", func(msg *nats.Msg) { var message = Message{} err := json.Unmarshal(msg.Data, &message) if err != nil { log.Printf("%v", err) return } if message.Sender != "bot" { // do something with user message and send back response nc.Publish("chat."+message.Sender, []byte(`{"message":"hello, i am bot. You send me: `+message.Message+`", "sender":"bot"}`)) } }) _ = nc chatIndex := func(w http.ResponseWriter, r *http.Request) { _, err := upsertSessionID(session, r, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } chatview.Index().Render(r.Context(), w) } chatMessage := func(w http.ResponseWriter, r *http.Request) { id, err := upsertSessionID(session, r, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var state = Message{} err = datastar.BodyUnmarshal(r, &state) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } state.Sender = id b, err := json.Marshal(state) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } nc.Publish("chat."+id, b) sse := datastar.NewSSE(w, r) _ = sse } chatMessages := func(w http.ResponseWriter, r *http.Request) { id, err := upsertSessionID(session, r, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var ch = make(chan *nats.Msg) sub, err := nc.Subscribe("chat."+id, func(msg *nats.Msg) { ch <- msg }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer close(ch) defer sub.Unsubscribe() sse := datastar.NewSSE(w, r) for { select { case <-r.Context().Done(): return case msg := <-ch: var message = Message{} err := json.Unmarshal(msg.Data, &message) if err != nil { datastar.Error(sse, err) return } if message.Sender == "bot" { datastar.RenderFragmentTempl(sse, chatview.Bot(message.Message), datastar.WithMergeAppend(), datastar.WithQuerySelector("#chatbox")) } else { datastar.RenderFragmentTempl(sse, chatview.Me(message.Message), datastar.WithMergeAppend(), datastar.WithQuerySelector("#chatbox")) } } } } router.Get("/chat", chatIndex) router.Post("/chat", chatMessage) router.Get("/messages", chatMessages) return nil } func upsertSessionID(store sessions.Store, r *http.Request, w http.ResponseWriter) (string, error) { sess, err := store.Get(r, "chatters") if err != nil { return "", fmt.Errorf("failed to get session: %w", err) } id, ok := sess.Values["id"].(string) if !ok { id = toolbelt.NextEncodedID() sess.Values["id"] = id if err := sess.Save(r, w); err != nil { return "", fmt.Errorf("failed to save session: %w", err) } } return id, nil }
とテンプレート
package chatview import "github.com/blinkinglight/chat-data-star/web/views/layoutview" templ Index() { @layoutview.Main() { <!-- component --> <div class="fixed bottom-0 right-0 mb-4 mr-4"> <button id="open-chat" class="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 transition duration-300 flex items-center"> <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path> </svg> Chat with Admin Bot </button> </div> <div id="chat-container" class="hidden fixed bottom-16 right-4 w-96"> <div class="bg-white shadow-md rounded-lg max-w-lg w-full"> <div class="p-4 border-b bg-blue-500 text-white rounded-t-lg flex justify-between items-center"> <p class="text-lg font-semibold">Admin Bot</p> <button id="close-chat" class="text-gray-300 hover:text-gray-400 focus:outline-none focus:text-gray-400"> <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path> </svg> </button> </div> <div id="chatbox" class="p-4 h-80 overflow-y-auto" data-on-load="$$get('/messages')" data-store="{ message: '' }"> <!-- Chat messages will be displayed here --> </div> <div class="p-4 border-t flex"> <input data-model="message" id="user-input" type="text" placeholder="Type a message" class="w-full px-3 py-2 border rounded-l-md focus:outline-none focus:ring-2 focus:ring-blue-500"/> <button data-on-keydown.window.key_enter="$$post('/chat'); $message=''" data-on-click="$$post('/chat'); $message=''" id="send-button" class="bg-blue-500 text-white px-4 py-2 rounded-r-md hover:bg-blue-600 transition duration-300">Send</button> </div> </div> </div> <script> const chatbox = document.getElementById("chatbox"); const chatContainer = document.getElementById("chat-container"); const userInput = document.getElementById("user-input"); const sendButton = document.getElementById("send-button"); const openChatButton = document.getElementById("open-chat"); const closeChatButton = document.getElementById("close-chat"); let isChatboxOpen = true; // Set the initial state to open function toggleChatbox() { chatContainer.classList.toggle("hidden"); isChatboxOpen = !isChatboxOpen; // Toggle the state } openChatButton.addEventListener("click", toggleChatbox); closeChatButton.addEventListener("click", toggleChatbox); toggleChatbox(); </script> } } templ Me(message string) { <div class="mb-2 text-right"> <p class="bg-blue-500 text-white rounded-lg py-2 px-4 inline-block">{ message }</p> </div> } templ Bot(message string) { <div class="mb-2"> <p class="bg-gray-200 text-gray-700 rounded-lg py-2 px-4 inline-block">{ message }</p> </div> }
https://github.com/blinkinglight/chat-data-star で実際の例を見つけることができます
以上がデータスターを使用したボットとのリアルタイムチャットの詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國語 Web サイトの他の関連記事を參照してください。

ホットAIツール

Undress AI Tool
脫衣畫像を無料で

Undresser.AI Undress
リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover
寫真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中國語版
中國語版、とても使いやすい

ゼンドスタジオ 13.0.1
強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック

Golangofferssuperiorporformance、nativeconconcurrencyviagoroutines、および効率的なresourceusage、makingitidealforhigh-raffic、low-latencyapis;

Golangは主にバックエンド開発に使用されますが、フロントエンドフィールドで間接的な役割を果たすこともできます。その設(shè)計(jì)目標(biāo)は、高性能、同時(shí)処理、システムレベルのプログラミングに焦點(diǎn)を當(dāng)てており、APIサーバー、マイクロサービス、分散システム、データベース操作、CLIツールなどのバックエンドアプリケーションの構(gòu)築に適しています。 GolangはWebフロントエンドの主流言語ではありませんが、Gopherjsを介してJavaScriptにコンパイルしたり、Tinygoを介してWebAssemblyで実行したり、テンプレートエンジンを備えたHTMLページを生成してフロントエンド開発に參加できます。ただし、最新のフロントエンド開発は、JavaScript/TypeScriptとそのエコシステムに依存する必要があります。したがって、Golangは、コアとして高性能バックエンドを備えたテクノロジースタック選択により適しています。

GOでGraphQlapiを構(gòu)築するには、GQLGenライブラリを使用して開発効率を向上させることをお勧めします。 1.最初に、スキーマに基づいた自動コード生成をサポートするGQLGENなどの適切なライブラリを選択します。 2。次に、graphqlschemaを定義し、投稿の種類やクエリメソッドの定義など、API構(gòu)造とクエリポータルを説明します。 3。次に、プロジェクトを初期化し、基本コードを生成して、リゾルバにビジネスロジックを?qū)g裝します。 4.最後に、graphqlhandlerをhttpserverに接続し、組み込みの遊び場を介してAPIをテストします。メモには、プロジェクトのメンテナンスを確保するためのフィールドネーミング仕様、エラー処理、パフォーマンスの最適化、セキュリティ設(shè)定が含まれます

GOをインストールするための鍵は、正しいバージョンを選択し、環(huán)境変數(shù)を構(gòu)成し、インストールを検証することです。 1.公式Webサイトにアクセスして、対応するシステムのインストールパッケージをダウンロードします。 Windowsは.msiファイルを使用し、macosは.pkgファイルを使用し、Linuxは.tar.gzファイルを使用し、 /usr /localディレクトリに解凍します。 2.環(huán)境変數(shù)を構(gòu)成し、linux/macOSで?/.bashrcまたは?/.zshrcを編集してパスとgopathを追加し、Windowsがシステムプロパティに移動するパスを設(shè)定します。 3.政府コマンドを使用してインストールを確認(rèn)し、テストプログラムを?qū)g行してhello.goを?qū)g行して、編集と実行が正常であることを確認(rèn)します。プロセス全體のパス設(shè)定とループ

Golangは通常、Webサービスを構(gòu)築するときにPythonよりもCPUとメモリを消費(fèi)します。 1. GolangのGoroutineモデルは、スケジューリングに効率的であり、強(qiáng)力なリクエスト処理機(jī)能を備えており、CPUの使用量が少ない。 2。GOはネイティブコードにコンパイルされ、ランタイム中に仮想マシンに依存せず、メモリの使用量が小さくなります。 3. Pythonは、GILと解釈実行メカニズムのために、同時(shí)シナリオでより大きなCPUとメモリオーバーヘッドを持っています。 4. Pythonには高い開発効率と豊富なエコシステムがありますが、低リソースを消費(fèi)します。これは、並行性要件が低いシナリオに適しています。

sync.waitgroupは、ゴルチンのグループがタスクを完了するのを待つために使用されます。そのコアは、3つの方法で協(xié)力することです。追加、完了、待機(jī)です。 1.ADD(n)待機(jī)するゴルチンの數(shù)を設(shè)定します。 2.done()は各ゴルチンの端で呼び出され、カウントは1つ減少します。 3.wait()すべてのタスクが完了するまでメインコルーチンをブロックします。使用する場合は、注意してください。Goroutineの外部で追加する必要があります。重複を避け、Donが呼び出されていることを確認(rèn)してください。 Deferで使用することをお勧めします。これは、Webページの同時(shí)クロール、バッチデータ処理、その他のシナリオで一般的であり、並行性プロセスを効果的に制御できます。

Goの埋め込みパッケージを使用すると、靜的リソースをバイナリに簡単に埋め込み、Webサービスに適しており、HTML、CSS、寫真、その他のファイルをパッケージ化できます。 1。追加する埋め込みリソースを宣言します// go:embed comment hello.txtを埋め込むなど、変數(shù)の前に埋め込みます。 2。static/*などのディレクトリ全體に埋め込み、embed.fsを介してマルチファイルパッケージを?qū)g現(xiàn)できます。 3.効率を改善するために、ビルドタグまたは環(huán)境変數(shù)を介してディスクロードモードを切り替えることをお勧めします。 4.パスの精度、ファイルサイズの制限、埋め込みリソースの読み取り専用特性に注意してください。埋め込みの合理的な使用は、展開を簡素化し、プロジェクト構(gòu)造を最適化することができます。

COOSIONGOOSIONGOOSIONGOOSIONGOOSIONGOOSIONGONDPYTHONFORCODEREADANIVERATIONALIGHT、theSecisionHingSonteampriolities.1.GolangOfferStrictStrictConsinceWithMinimal、意見を述べたsyntaxandaxandaxandaxandisinglikelikelikegofmt、enuringuniformcodestylecection.2.pythertection.2
