337 lines
8.0 KiB
HTML
337 lines
8.0 KiB
HTML
<!DOCTYPE HTML>
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta http-equiv="Expires" content="0">
|
||
<meta http-equiv="Pragma" content="no-cache">
|
||
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
|
||
<meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0"/>
|
||
<title>后台玩家查询</title>
|
||
</head>
|
||
|
||
<body>
|
||
<div id="divChat">
|
||
<textarea name="txtChatMain" id="txtChatMain" readonly="readonly" class="txtarea"></textarea>
|
||
</div>
|
||
|
||
<style type='text/css'>
|
||
div#divChat { width:30em; }
|
||
textarea.txtarea { float:left; width: 34em; height: 20em;}
|
||
</style>
|
||
<script type="text/javascript" src="indexDb.js"></script>
|
||
<script type="text/javascript" src="minhttp.js"></script>
|
||
<script type="text/javascript" src="packet.js"></script>
|
||
<script type="text/javascript">
|
||
// Websocket object
|
||
var ws = null;
|
||
// Websocket 连接地址
|
||
var ws_url = "ws://127.0.0.1:31088";
|
||
// Helper functions
|
||
function $() {
|
||
return document.getElementById(arguments[0]);
|
||
}
|
||
function logMsg(msg) {
|
||
$('txtChatMain').value += msg + '\n';
|
||
}
|
||
function logmsg(msg) {
|
||
$('txtChatMain').value += msg + '\n';
|
||
}
|
||
function sendMsg(msg) {
|
||
ws.send(msg);
|
||
}
|
||
function sendmsg(msg) {
|
||
ws.send(msg);
|
||
}
|
||
//重新连接
|
||
function reconnect(){
|
||
ws = null;
|
||
connectWebSocket();
|
||
}
|
||
// Websocket functions
|
||
function connectWebSocket() {
|
||
OnBeginConnectedServerChat();
|
||
if ("WebSocket" in window) {
|
||
// Create new websocket connection
|
||
ws = new WebSocket(ws_url);
|
||
|
||
// Called after connection is established
|
||
ws.onopen = function() {
|
||
ws.wsisclose = false;
|
||
logMsg(min_now() + ' tcp connected to: ' + ws.url);
|
||
sendMsg('dfw19770109'); //告诉ServerChat.exe我是逻辑服务器
|
||
};
|
||
|
||
// Called when connection is closed
|
||
ws.onclose = function() {
|
||
ws.wsisclose = true;
|
||
logMsg(min_now() + " Connection closed!");
|
||
ws.reconnecttimer = min_ontimeout(reconnect, 5000);
|
||
};
|
||
|
||
// Called when a new message is received
|
||
ws.sendall = function (msg) {
|
||
if (typeof(msg) == 'string') {
|
||
sendMsg('@toallall:' + msg);
|
||
}
|
||
else
|
||
{
|
||
var s = JSON.stringify(msg);
|
||
sendMsg('@toallall:' + s);
|
||
}
|
||
}
|
||
|
||
//Send message to HTTP client
|
||
ws.sendtou = function (head, msg) {
|
||
if (ws.wsisclose){
|
||
return;
|
||
}
|
||
sendMsg(head + msg);
|
||
}
|
||
|
||
//Send message to TCP client
|
||
ws.sendbyids = function(ids, msg) {
|
||
if (ws.wsisclose){
|
||
return;
|
||
}
|
||
var s = '@tooneone:';
|
||
var str_ids;
|
||
if (typeof(msg) == 'string')
|
||
{}
|
||
else
|
||
{
|
||
msg = JSON.stringify(msg);
|
||
}
|
||
if (typeof(ids) == 'string')
|
||
{
|
||
str_ids = ids;
|
||
}
|
||
else
|
||
{
|
||
//ids:[1,5,8] 要转成字符串 1,5,8
|
||
str_ids = ids[0];
|
||
for (var i=1; i<ids.length; i++)
|
||
{
|
||
str_ids = str_ids + ',' + ids[i];
|
||
}
|
||
}
|
||
s = s + str_ids + ':' + msg;
|
||
sendMsg(s);
|
||
}
|
||
|
||
ws.sendclosetcp = function(ids) {
|
||
if (ws.wsisclose){
|
||
return;
|
||
}
|
||
var s = '@toclosed:';
|
||
var str_ids;
|
||
if (typeof(ids) == 'string') {
|
||
str_ids = ids;
|
||
}else{
|
||
//ids:[1,5,8] 要转成字符串 1,5,8
|
||
str_ids = ids[0];
|
||
for (var i=1; i<ids.length; i++)
|
||
{
|
||
str_ids = str_ids + ',' + ids[i];
|
||
}
|
||
}
|
||
s = s + str_ids+ ':a';
|
||
sendMsg(s);
|
||
}
|
||
|
||
ws.onmessage = function (msg) {
|
||
if (msg.data)
|
||
{
|
||
// logMsg(msg.data);
|
||
var arr = msg.data;
|
||
var tou = arr.substr(10, 5);
|
||
// var arrs = arr.split(":");
|
||
//取前面50个字符,以免全字符split,提高效率
|
||
var arr50 = arr.substr(0, 50);
|
||
var arrs = arr50.split(":");
|
||
|
||
if (tou == 'http_')
|
||
{//http
|
||
var s1 = '@tooneone' + ':' + arrs[1] + ':';
|
||
var s2 = "";
|
||
if (arrs.length > 2)
|
||
{//是数据,否则是命令
|
||
s2 = msg.data.substr(s1.length, msg.data.length-s1.length);
|
||
}
|
||
OnHttpMessage(s1, s2);
|
||
}
|
||
else
|
||
{//tcp
|
||
var ss0 = arrs[0];
|
||
var ss1 = arrs[1];
|
||
var sip = arrs[2];
|
||
var s01 = ss0 + ':' + ss1 + ':' + sip + ':';
|
||
var ss2 = "";
|
||
if (arrs.length > 3)
|
||
{//是数据,否则是命令
|
||
ss2 = msg.data.substr(s01.length, msg.data.length - s01.length);
|
||
}
|
||
OnTcpMessage(ss0, ss1, sip, ss2);
|
||
|
||
// var ss0 = arrs[0];
|
||
// var ss1 = arrs[1];
|
||
// var s01 = ss0 + ':' + ss1 + ':';
|
||
// var ss2 = "";
|
||
// if (arrs.length > 2)
|
||
// {//是数据,否则是命令
|
||
// ss2 = msg.data.substr(s01.length, msg.data.length - s01.length);
|
||
// }
|
||
// OnTcpMessage(ss0, ss1, "", ss2);
|
||
}
|
||
};
|
||
|
||
return;
|
||
};
|
||
}
|
||
else
|
||
{
|
||
logMsg('Browser doesn\'t support websockets!');
|
||
}
|
||
|
||
OnAfterConnectedServerChat();
|
||
}
|
||
|
||
//server.html连接ServerChat.exe前的事件
|
||
function OnBeginConnectedServerChat()
|
||
{
|
||
}
|
||
|
||
//server.html连接ServerChat.exe后的事件
|
||
function OnAfterConnectedServerChat()
|
||
{
|
||
//给packet_face传递发包函数
|
||
if (typeof(packet_face) != "undefined")
|
||
{
|
||
global.packet_face.SendPack_Tcp = ws.sendbyids;
|
||
global.packet_face.SendPack_Http = ws.sendtou;
|
||
}
|
||
}
|
||
|
||
//收到客户端http发包触发的事件
|
||
function OnHttpMessage(str_httpid, str_data)
|
||
{
|
||
try
|
||
{
|
||
str_data = min_replaceAll(str_data, "%", "");
|
||
str_data = decodeURIComponent(str_data); //解码
|
||
}
|
||
catch(e)
|
||
{
|
||
console.log("OnHttpMessage_decodeURIComponent:" + str_data);
|
||
return;
|
||
}
|
||
|
||
var s = "";
|
||
s = str_data.substr(0, 6);
|
||
if (s == "&data=") {
|
||
str_data = str_data.replace("&data=", "");
|
||
}
|
||
s = str_data.substr(0, 5);
|
||
if (s == "data=") {
|
||
str_data = str_data.replace("data=", "");
|
||
}
|
||
s = str_data.substr(0, 2);
|
||
if (s == "&=") {
|
||
str_data = str_data.replace("&=", "");
|
||
}
|
||
s = str_data.substr(0, 1);
|
||
if (s == "&") {
|
||
str_data = str_data.replace("&", "");
|
||
}
|
||
|
||
if (typeof(packet_face) != "undefined") {
|
||
if (str_data) {
|
||
try
|
||
{
|
||
var json_data = JSON.parse(str_data);
|
||
if (json_data){
|
||
json_data.conmode = "http";
|
||
json_data.fromid = str_httpid;
|
||
packet_face.ReceivePack(json_data);
|
||
}
|
||
}
|
||
catch(e)
|
||
{
|
||
console.log(min_now() + " str_data:" + str_data);
|
||
console.log(e);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//收到客户端tcp发包触发的事件
|
||
function OnTcpMessage(str_com, str_tcpid, str_ip, str_data)
|
||
{
|
||
switch (str_com)
|
||
{
|
||
case "@toconcon": //连上ServerChat.exe
|
||
break;
|
||
|
||
case "@tologin1": //客户端连接上
|
||
if (typeof(packet_face) != "undefined")
|
||
{
|
||
packet_face.OnTcpConnected(str_tcpid);
|
||
}
|
||
break;
|
||
|
||
case "@toleave1": //客户端断开
|
||
if (typeof(packet_face) != "undefined")
|
||
{
|
||
packet_face.OnTcpDisConnected(str_tcpid);
|
||
}
|
||
break;
|
||
|
||
case "@toserver": //客户端发包
|
||
if (typeof(packet_face) != "undefined")
|
||
{
|
||
try
|
||
{
|
||
str_data = min_replaceAll(str_data, "%", ""); //去除%,否则decodeURIComponent(str_data)会报错
|
||
str_data = decodeURIComponent(str_data); //解码
|
||
var json_data = JSON.parse(str_data);
|
||
if (json_data){
|
||
json_data.conmode = "tcp";
|
||
json_data.fromid = str_tcpid;
|
||
json_data.ip = str_ip;
|
||
packet_face.ReceivePack(json_data);
|
||
}
|
||
}
|
||
catch(e)
|
||
{
|
||
console.log(e);
|
||
var errlog = {};
|
||
errlog.name = "非法的TCP连接";
|
||
errlog.time = min_now();
|
||
errlog.com = str_com;
|
||
errlog.tcpid = str_tcpid;
|
||
errlog.ip = str_ip;
|
||
errlog.data = str_data;
|
||
console.log(errlog);
|
||
ws.sendclosetcp(str_tcpid);
|
||
}
|
||
}
|
||
break;
|
||
|
||
default:
|
||
var errlog = {};
|
||
errlog.name = "非法的TCP连接";
|
||
errlog.time = min_now();
|
||
errlog.com = str_com;
|
||
errlog.tcpid = str_tcpid;
|
||
errlog.ip = str_ip;
|
||
errlog.data = str_data;
|
||
console.log(errlog);
|
||
ws.sendclosetcp(str_tcpid);
|
||
break;
|
||
}
|
||
}
|
||
|
||
window.onload = connectWebSocket();
|
||
</script>
|
||
</body>
|
||
</html>
|