#Go言語によるWebアプリケーション開発にてチャットアプリが動作しない
##現状
- 「Go言語によるWebアプリケーション開発」にて、一章によるチャットアプリの作成にて、messageを送信しても反映されません
- 送信はされるが「localhost:8080/?」と表示され、その辺の応答がいまいちです
##ソースコード
html
1//chat.html 2<!DOCTYPE html> 3<html> 4 <head> 5 <title>チャット</title> 6 <style> 7 input { display: block; } 8 ul { list-style: none; } 9 </style> 10 </head> 11 <body> 12 <ul id="messages"></ul> 13 WebSocketを使ったチャットアプリ 14 <form id="chatbox"> 15 <textarea></textarea> 16 <input type="submit" value="send" /> 17 </form> 18 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 19 <script> 20 $(function(){ 21 var socket = null; 22 var msgBox = $("#chatbox textarea"); 23 var messages = $("#message"); 24 $("#chatbox").submit(function(){ 25 if (!msgBox.val()) return false; 26 if (!socket) { 27 alert("エラー:Websocket接続"); 28 return false; 29 } 30 socket.send(msgBox.val()); 31 msgBox.val(""); 32 return false; 33 }); 34 if(!window["WebSocket"]){ 35 alert("エラー:Websocketブラウザ"); 36 }else{ 37 socket = new WebSocket("ws://localhost:8080/room"); 38 socket.onclose = function() { 39 alert("接続の終了だお"); 40 } 41 socket.onmessage = function(e) { 42 messages.append($("<li>").text(e.data)); 43 } 44 } 45 }); 46 </script> 47 </body> 48</html>
go
1//main.go 2package main 3 4import ( 5 "log" 6 "net/http" 7 "path/filepath" 8 "sync" 9 "text/template" 10) 11 12type templateHandler struct { 13 once sync.Once 14 filename string 15 templ *template.Template 16} 17 18func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 19 t.once.Do(func() { 20 t.templ = 21 template.Must(template.ParseFiles(filepath.Join("templates", t.filename))) 22 }) 23 t.templ.Execute(w, nil) 24} 25 26func main() { 27 r := newRoom() 28 http.Handle("/", &templateHandler{filename: "chat.html"}) 29 http.Handle("/room", r) 30 go r.run() 31 if err := http.ListenAndServe(":8080", nil); err != nil { 32 log.Fatal("ListenAndServe:", err) 33 } 34} 35
go
1//client.go 2package main 3 4import ( 5 "github.com/gorilla/websocket" 6) 7 8type client struct { 9 socket *websocket.Conn 10 send chan []byte 11 room *room 12} 13 14func (c *client) read() { 15 for { 16 if _, msg, err := c.socket.ReadMessage(); err == nil { 17 c.room.forward <- msg 18 } else { 19 break 20 } 21 } 22 c.socket.Close() 23} 24 25func (c *client) write() { 26 for msg := range c.send { 27 if err := c.socket.WriteMessage(websocket.TextMessage, msg); err != nil { 28 break 29 } 30 } 31 c.socket.Close() 32} 33
go
1//room.go 2package main 3 4import ( 5 "log" 6 "net/http" 7 8 "github.com/gorilla/websocket" 9) 10 11type room struct { 12 forward chan []byte 13 join chan *client 14 leave chan *client 15 clients map[*client]bool 16} 17 18func newRoom() *room { 19 return &room{ 20 forward: make(chan []byte), 21 join: make(chan *client), 22 leave: make(chan *client), 23 clients: make(map[*client]bool), 24 } 25} 26 27func (r *room) run() { 28 for { 29 select { 30 case client := <-r.join: 31 r.clients[client] = true 32 case client := <-r.leave: 33 delete(r.clients, client) 34 close(client.send) 35 case msg := <-r.forward: 36 for client := range r.clients { 37 select { 38 case client.send <- msg: 39 default: 40 delete(r.clients, client) 41 close(client.send) 42 } 43 } 44 } 45 } 46} 47 48const ( 49 socketBufferSize = 1024 50 messageBufferSize = 256 51) 52 53var upgrader = &websocket.Upgrader{ReadBufferSize: socketBufferSize, WriteBufferSize: socketBufferSize} 54 55func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) { 56 socket, err := upgrader.Upgrade(w, req, nil) 57 if err != nil { 58 log.Fatal("ServeHTTP:", err) 59 return 60 } 61 client := &client{ 62 socket: socket, 63 send: make(chan []byte, messageBufferSize), 64 room: r, 65 } 66 67 r.join <- client 68 defer func() { r.leave <- client }() 69 go client.write() 70 client.read() 71} 72
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。