Go言語もWebSocketも初心者です。
下記Qiitaの記事を見ながら、
GolangとWebsocketを使って簡単なチャットアプリを作成しようとしております!
http://qiita.com/m0a/items/f6405bc29073a7609050
最初に画面を読み込んだタイミングで
JSON形式のテストデータ、test = {X:"x",Y:"y",Color:"color",Str:"str"};をサーバに送って、
画面に返ってくるとランダムの位置にそれが表示されるというのをやろうとしているのですが、
なぜかtestデータが返ってくるのと同時に接続が切れてしまいます。
接続がきれないようにするためにはどうしたらよろしいでしょうか??
コードは下記になります。
Go
1package main 2 3import ( 4 "code.google.com/p/go.net/websocket" 5 "log" 6 "net/http" 7) 8 9func echoHandler(ws *websocket.Conn) { 10 type T struct { 11 X int64 12 Y int64 13 Color string 14 Str string 15 } 16 17 var data T 18 websocket.JSON.Receive(ws, &data) 19 20 log.Printf("%#v\n", data) 21 22 websocket.JSON.Send(ws, data) 23} 24 25func main() { 26 http.Handle("/echo", websocket.Handler(echoHandler)) 27 http.Handle("/", http.FileServer(http.Dir("./"))) 28 if err := http.ListenAndServe(":8000", nil); err != nil { 29 panic("ListenAndServe: " + err.Error()) 30 } 31}
html
1<html> 2<head> 3 <meta charset='utf-8'> 4 <title>#saikin_go</title> 5 <link rel="stylesheet" href="css/index.css"> 6 <script type="text/javascript"> 7 //var wsUri = "ws://echo.websocket.org/"; 8 var wsUri = "ws://localhost:8000/echo"; 9 10 var output; 11 function init() { 12 output = document.getElementById("output") 13 testWebSocket(); 14 } 15 16 function testWebSocket() { 17 websocket = new WebSocket(wsUri); 18 websocket.onopen = function(evt) { 19 onOpen(evt) 20 }; 21 websocket.onclose = function(evt) { 22 onClose(evt) 23 }; 24 websocket.onmessage = function(evt) { 25 onMessage(evt) 26 }; 27 websocket.onerror = function(evt) { 28 onError(evt) 29 }; 30 } 31 32 function onOpen(evt) { 33 var test = {X:"x",Y:"y",Color:"color",Str:"str"}; 34 doSend(test); 35 } 36 function onClose(evt) { 37 writeToScreen("DISCONNECTED"); 38 } 39 40 function onMessage(evt) { 41 chatAppear(evt.data); 42 } 43 44 function onError(evt) { 45 writeToScreen('<span style="color: red;">ERROR:</span>'+ evt.data); 46 } 47 48 function doSend(message) { 49 websocket.send(JSON.stringify(message));; 50 } 51 52 function writeToScreen(message) { 53 var pre = document.createElement("p"); 54 pre.style.wordWrap = "break-word"; 55 pre.innerHTML = message; 56 output.appendChild(pre); 57 } 58 59 function sendChat(){ 60 var x = Math.floor(Math.random() * 1024); 61 var y = Math.floor(Math.random() * 512); 62 var colors = ['Red','Yellow','Blue','LawnGreen'] 63 var color = colors[Math.floor(Math.random() * 4)]; 64 var str = document.querySelector("#chatmessage").value; 65 var state = {X:x,Y:y,Color:color,Str:str}; 66 doSend(state); 67 str.value = ""; 68 } 69 70 function chatAppear(str){ 71 var colors = ['Red','Yellow','Blue','LawnGreen'] 72 var canvas_zone = document.getElementById('canvas_zone'); 73 var tweet = document.createElement('div'); 74 var tweet_content = document.createTextNode(str) 75 tweet.appendChild(tweet_content); 76 tweet.style.padding = '10px'; 77 tweet.style.borderRadius = '4px'; 78 tweet.className = 'tweets'; 79 tweet.style.position = 'absolute'; 80 tweet.style.backgroundColor = colors[Math.floor(Math.random() * 4)]; 81 tweet.style.top = Math.floor(Math.random() * 512)+'px'; 82 tweet.style.left = Math.floor(Math.random() * 1024)+'px'; 83 canvas_zone.appendChild(tweet); 84 } 85 86 window.addEventListener("load", init, false); 87 88 </script> 89 90 </head> 91 <body> 92 <h2>#saikin_go</h2> 93 <div class="join_zone"> 94 <input id="chatmessage" type="text" value="@"/> 95 <input type="button" value="send" onClick="sendChat()" /> 96 <div id="output"></div> 97 </div> 98 <div id="canvas_zone"></div> 99 </body> 100 </html>
何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/27 15:12