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
package main | |
import ( | |
"code.google.com/p/go.net/websocket" | |
"net/http" | |
"io" | |
"log" | |
) | |
func echoHandler(ws *websocket.Conn) { |
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 | |
} |
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 Message struct { | |
// 著者(送り主) | |
Author string `json:"author"` | |
// メッセージ本文 | |
Body string `json:"body"` | |
} | |
func (self *Message) String() string { | |
return self.Author + " says " + self.Body |
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 |
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
// サーバを起動します。 | |
func (self *Server) Listen() { | |
log.Println("Listening server...") | |
// クライアントが接続されたときに呼ばれるハンドラを作ります。 | |
onConnected := func(ws *websocket.Conn) { | |
client := NewClient(ws, self) | |
self.addClient <- client | |
client.Listen() | |
defer ws.Close() |
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
// 書き込み処理と読み込み処理を待ちます。 | |
func (self *Client) Listen() { | |
go self.listenWrite() | |
self.listenRead() | |
} | |
// 書き込み処理を待ちます。 | |
func (self *Client) listenWrite() { | |
log.Println("Listening write to client") |
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 Client struct { | |
// Websocketの接続情報 | |
ws *websocket.Conn | |
// サーバ | |
server *Server | |
// クライアントに送るメッセージを受け取るチャネル | |
ch chan *Message | |
// 終了リクエストを受け取るチャネル | |
done chan bool |
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
// http://play.golang.org/p/baw3-Q0JFH | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func main() { |
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
// http://play.golang.org/p/CFA11n1BF6 | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
) | |
func main() { | |
for i := 0; i < 10; i++ { |
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
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
select { | |
case m := <-c: |