Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Last active December 10, 2015 02:39
Show Gist options
  • Save tenntenn/4369437 to your computer and use it in GitHub Desktop.
Save tenntenn/4369437 to your computer and use it in GitHub Desktop.
// チャットサーバ
type Server struct {
// ハンドラのパス
path string
// クライアント
clients []*Client
// クライアント追加リクエストのチャネル
addClient chan *Client
// クライアント削除リクエストのチャネル
removeClient chan *Client
// ブロードキャストリクエストのチャネル
sendAll chan *Message
// 今までのブロードキャストしたメッセージ
messages []*Message
}
// サーバを作成します。
func NewServer(path string) *Server {
clients := make([]*Client, 0)
addClient := make(chan *Client)
removeClient := make(chan *Client)
sendAll := make(chan *Message)
messages := make([]*Message, 0)
return &Server{path, clients, addClient, removeClient, sendAll, messages}
}
// クライアント追加リクエストのチャネルを取得します。
func (self *Server) AddClient() chan<- *Client {
return (chan<- *Client)(self.addClient)
}
// クライアント削除リクエストのチャネルを取得します。
func (self *Server) RemoveClient() chan<- *Client {
return (chan<- *Client)(self.removeClient)
}
// ブロードキャストリクエストのチャネルを取得します。
func (self *Server) SendAll() chan<-*Message {
return (chan<-*Message)(self.sendAll)
}
// 今までメッセージを取得します。
func (self *Server) Messages() []*Message {
msgs := make([]*Message, len(self.messages))
copy(msgs, self.messages)
return msgs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment