目录结构调整
This commit is contained in:
381
codes/games/server/YouLeGamesServer.html
Normal file
381
codes/games/server/YouLeGamesServer.html
Normal file
@@ -0,0 +1,381 @@
|
||||
<!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="md5.js"></script>
|
||||
<script type="text/javascript" src="indexDb.js"></script>
|
||||
<script type="text/javascript" src="lz-string.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:3088";
|
||||
//临时屏蔽某个游戏的数据包
|
||||
var blacknamelist_game = [];
|
||||
// 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) {
|
||||
try{
|
||||
ws.send(msg);
|
||||
}catch(e){
|
||||
console.log(min_now() + "发包报错。");
|
||||
console.log(msg);
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
function sendmsg(msg) {
|
||||
try{
|
||||
ws.send(msg);
|
||||
}catch(e){
|
||||
console.log(min_now() + "发包报错。");
|
||||
console.log(msg);
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
//重新连接
|
||||
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() {
|
||||
logMsg(min_now() + ' tcp connected to: ' + ws.url);
|
||||
sendMsg('dfw19770109'); //告诉ServerChat.exe我是逻辑服务器
|
||||
ws.wsisclose = false;
|
||||
};
|
||||
|
||||
// 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 (ws.wsisclose){
|
||||
return;
|
||||
}
|
||||
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
|
||||
{
|
||||
//去除%,否则decodeURIComponent(str_data)会报错
|
||||
str_data = min_replaceAll(str_data, "%", "");
|
||||
//解码
|
||||
str_data = decodeURIComponent(str_data);
|
||||
var json_data = JSON.parse(str_data);
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
console.log(min_now() + "非法的TCP连接");
|
||||
var errlog = {};
|
||||
errlog.com = str_com;
|
||||
errlog.tcpid = str_tcpid;
|
||||
errlog.ip = str_ip;
|
||||
errlog.data = str_data;
|
||||
console.log(errlog);
|
||||
//断开tcp连接
|
||||
ws.sendclosetcp(str_tcpid);
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_data){
|
||||
try
|
||||
{
|
||||
//屏蔽某个有错的route
|
||||
if (json_data.route){
|
||||
if (min_ary_indexof(blacknamelist_game, json_data.route) > -1){
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(e)
|
||||
{}
|
||||
|
||||
json_data.conmode = "tcp";
|
||||
json_data.fromid = str_tcpid;
|
||||
json_data.ip = str_ip;
|
||||
|
||||
try
|
||||
{
|
||||
packet_face.ReceivePack(json_data);
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
console.log(min_now() + "执行rpc出错");
|
||||
console.log(json_data);
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
console.log(min_now() + "非法的TCP连接");
|
||||
var errlog = {};
|
||||
errlog.com = str_com;
|
||||
errlog.tcpid = str_tcpid;
|
||||
errlog.ip = str_ip;
|
||||
errlog.data = str_data;
|
||||
console.log(errlog);
|
||||
//断开tcp连接
|
||||
ws.sendclosetcp(str_tcpid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
window.onload = connectWebSocket();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user