go代码:
package main
import (
"flag"
"fmt"
"glog-master"
"net/http"
"code.google.com/p/go.net/websocket"
)
// 初始化函数
func init() {
fmt.Println("Entry init")
// 连接池的 初始化; 数据结构的初始化;
PlayerBuShu = make(map[string]int)
H5_Data = make(map[string]*OnlineUser)
flag_h5 = 0
flag.Set("alsologtostderr", "true") // 日志写入文件的同时,输出到stderr
flag.Set("log_dir", "./tmp") // 日志文件保存目录
flag.Set("v", "3") // 配置V输出的等级。
flag.Parse()
return
}
// 主函数
func main() {
glog.Info("main")
fmt.Println("Entry main")
http.Handle("/Golangweb", websocket.Handler(BuildConnection))
if err := http.ListenAndServe(":7821", nil); err != nil {
fmt.Println("main err:", err.Error())
return
}
return
}
func BuildConnection(ws *websocket.Conn) {
data := ws.Request().URL.Query().Get("data")
if data == "" {
fmt.Println("data is nil!!!")
//return
}
// 接受到的 客户端的数据
fmt.Println("data:", data)
// 处理-- 用户的登陆 用户的基础行为(走路 , 开枪 ,死亡 等等)
onlineUser := &OnlineUser{
Connection: ws,
}
onlineUser.PullFromClient()
}
func (this *OnlineUser) PullFromClient() {
fmt.Println("PullFromClient")
for {
var content string
if err := websocket.Message.Receive(this.Connection, &content); err != nil {
break
}
if len(content) == 0 {
break
}
glog.Info("data:", content)
}
}
H5代码:
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Golang 语言社区 (www.Golang.Ltd)</title>
<body>
<h3 style="color: cornflowerblue;">Golang 语言社区 </h3> 用户名: <input type='text' id='name' value="yulegame">
<input type="submit" onclick="login()" />
</body>
<script language="javascript" type="text/javascript">
var wsUri = "ws://127.0.0.1:7821/Golangweb?data={'Protocol':3,'Protocol2':1,'OpenID'='ww'}";
var goServerJson;
var json;
function login() {
var test;
test = document.getElementById("name").value
alert(test);
var json = {
Protocol: 1,
Protocol2: 3,
};
var goServerJson = JSON.stringify(json);
wsUri = wsUri;
console.log(wsUri);
doSend(goServerJson);
}
//////////////////////////////////////////////////////////////////
var output;
var websocket1;
function init() {
output = document.getElementById("output");
testWebSocket();
}
function testWebSocket() {
console.log(wsUri)
websocket1 = new WebSocket(wsUri);
websocket1.onopen = function(evt) {
onOpen(evt)
};
websocket1.onclose = function(evt) {
onClose(evt)
};
websocket1.onmessage = function(evt) {
onMessage(evt)
};
websocket1.onerror = function(evt) {
onError(evt)
};
}
function onOpen(evt) {
writeToScreen("CONNECTED");
}
function onClose(evt) {
writeToScreen("DISCONNECTED");
}
function onMessage(evt) {
var data = evt.data;
console.log("data=" + data);
var d = base64decode(data);
var zhuana = utf8to16(d);
writeToScreen('<span style="color: blue;">RESPONSE: ' + zhuana + '</span>');
}
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message) {
writeToScreen("SENT: " + message);
writeToScreen('<span style="color: blue;">RESPONSE: ' + base64encode(message) + '</span>');
websocket1.send(message);
}
function writeToScreen(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
</script>
<script src="js/luanma.js"></script>
<div id="output"></div>
服务器日志截图:
代码下载链接:http://www.golang.ltd/forum.php?mod=viewthread&tid=5992