Last active
December 10, 2015 02:39
-
-
Save tenntenn/4369437 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// チャットサーバ | |
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