Coder Social home page Coder Social logo

grpc-chat-go's Introduction

gRPC-chat-go

Run

go run cmd/main.go

Access gRPC

grpcurl -plaintext localhost:8080 list
grpcurl -plaintext localhost:8080 list [サービス名]
grpcurl -plaintext -d '[メッセージ]' localhost:8080 [メソッド名]

記事化メモ

GoでWebsockeを使用してチャットを作ろうとしたら詰まったこと

  • 元々参考にしていた記事

https://zenn.dev/tady/articles/adcdc65617ae57

  • chatgptに聞いた回答

goでwebsocket通信を使用したチャットを作ってください。 ただし、使用するライブラリは、「"golang.org/x/net/websocket"」でお願いします。 ChatGPT もちろんです。以下はGo言語を使用してWebSocket通信を利用した簡単なチャットアプリケーションの例です。この例では、golang.org/x/net/websocket パッケージを使用します。

まず、main.go ファイルを作成し、以下のコードを追加します:

package main

import (
	"fmt"
	"golang.org/x/net/websocket"
	"net/http"
)

// チャットルーム構造体
type ChatRoom struct {
	clients map[*websocket.Conn]bool
	messages chan string
}

// メッセージのブロードキャスト
func (c *ChatRoom) broadcast(message string) {
	for client := range c.clients {
		websocket.Message.Send(client, message)
	}
}

// WebSocketハンドラ
func (c *ChatRoom) handleWebSocket(conn *websocket.Conn) {
	c.clients[conn] = true
	defer func() {
		delete(c.clients, conn)
		conn.Close()
	}()

	var message string
	for {
		err := websocket.Message.Receive(conn, &message)
		if err != nil {
			break
		}
		c.messages <- message
	}
}

// メッセージを受信しブロードキャスト
func (c *ChatRoom) processMessages() {
	for {
		message := <-c.messages
		c.broadcast(message)
	}
}

func main() {
	// チャットルームの初期化
	chatRoom := &ChatRoom{
    clients:  make(map[*websocket.Conn]bool),
	messages: make(chan string),
	}
	go chatRoom.processMessages()

	// WebSocketハンドラを設定
	http.Handle("/chat", websocket.Handler(func(ws *websocket.Conn) {
		chatRoom.handleWebSocket(ws)
	}))

	// サーバーを起動
	fmt.Println("WebSocketサーバーが :8080 で起動しました。")
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println(err)
	}
}

このコードでは、WebSocketを使用してクライアント間でメッセージを送受信できるシンプルなチャットルームを作成しています。

以下の手順で動作を確認できます:

ターミナルでプロジェクトのディレクトリに移動し、main.go ファイルを作成します。 上記のコードを main.go ファイルに貼り付けます。 コンソールで go run main.go を実行します。 WebSocketサーバーは localhost:8080 で起動されます。

これで、WebSocketを使用した簡単なチャットアプリケーションが完成しました。この例では、単純なブロードキャストのみを行っていますが、実際のアプリケーションではセキュリティやエラーハンドリングなど、さらなる機能が必要になるかもしれません。

grpc-chat-go's People

Contributors

o-ga09 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.