fixed some bugs in websocket demo

This commit is contained in:
shuxin   zheng 2022-02-10 09:50:00 +08:00
parent 2b9418a396
commit c968da6825
5 changed files with 8 additions and 100 deletions

View File

@ -95,7 +95,7 @@ bool http_servlet::doPost(acl::HttpServletRequest& req,
return ret; return ret;
} }
bool http_servlet::doWebsocket(acl::HttpServletRequest& req, bool http_servlet::doWebSocket(acl::HttpServletRequest& req,
acl::HttpServletResponse&) acl::HttpServletResponse&)
{ {
acl::socket_stream& ss = req.getSocketStream(); acl::socket_stream& ss = req.getSocketStream();

View File

@ -49,7 +49,7 @@ protected:
bool doPost(acl::HttpServletRequest&, acl::HttpServletResponse&); bool doPost(acl::HttpServletRequest&, acl::HttpServletResponse&);
// @override // @override
bool doWebsocket(acl::HttpServletRequest&, acl::HttpServletResponse&); bool doWebSocket(acl::HttpServletRequest&, acl::HttpServletResponse&);
private: private:
bool doPing(acl::websocket&, acl::websocket&); bool doPing(acl::websocket&, acl::websocket&);

View File

@ -25,7 +25,7 @@ int main(int argc, char *argv[])
addr = argv[3]; addr = argv[3];
printf("listen: %s\r\n", addr); printf("listen: %s\r\n", addr);
ms.run_alone(addr, argc >= 3 ? argv[2] : NULL, 0); ms.run_alone(addr, argc >= 3 ? argv[2] : NULL);
} }
else else
ms.run_daemon(argc, argv); ms.run_daemon(argc, argv);

View File

@ -1,93 +0,0 @@
<html>
<meta charset="utf-8" />
<title>WebSocket Chat</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://121.42.12.244:9001/";
var output = document.getElementById("output");
var ws = null;
function OnConnect(user) {
wsUri = document.getElementById("url").value;
openWebSocket(user);
}
function openWebSocket(user) {
if (ws != null)
ws.close();
ws = new WebSocket(wsUri);
ws.onmessage = function (evt) { onMessage(evt); };
ws.onopen = function (evt) { onOpen(evt, user); };
ws.onclose = function (evt) { onClose(evt); };
ws.onerror = function (evt) { onError(evt); };
}
function onMessage(evt) {
var data = evt.data + "<br>" + document.getElementById('result').innerHTML;
document.getElementById('result').innerHTML = data;
}
function onOpen(evt, user) {
// writeToScreen("CONNECTED");
}
function onClose(evt) {
// writeToScreen("DISCONNECTED");
}
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doLogin(user) {
var user = document.getElementById(user).value;
message = "login|" + user;
ws.send(message);
}
function doSend(to, msgid) {
var to_user = document.getElementById(to).value;
var msg = document.getElementById(msgid).value;
message = "chat|" + msg + "|" + to_user;
ws.send(message);
}
function doClear(result) {
document.getElementById(result).innerHTML = "";
}
function writeToScreen(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.insertBefore(pre);
}
</script>
<body>
<fieldset style="width:530px;">
<legend>WebSocket</legend>
<p><span>Server url:</span>
<input id="url" type="text" value="ws://121.42.12.244:9001/" />
<input type="button" value="Connect" onclick="OnConnect('user');" />
</p>
<p>
me: <input id="user" type="text" value="user" />
<input type="button" value="login" onclick="doLogin('user');" />
</p>
<p>
to: &nbsp;&nbsp;<input id="to_user" type="text" value="user" />
&nbsp;&nbsp;
msg: <input type=text id="msg" value="hello world!" />
<input type="button" value="send" onclick="doSend('to_user', 'msg');" />
<input type="button" value="clear" onclick="doClear('result');" />
</p>
recv:
<fieldset style="width:500px;height:80px; overflow:auto;">
<div id="result"></div>
</fieldset>
</fieldset>
<div id="output"> </div>
</body>
</html>

View File

@ -23,11 +23,11 @@
} }
function onOpen(evt, user) { function onOpen(evt, user) {
setStatus("connected ..."); setStatus("connected!");
} }
function onClose(evt) { function onClose(evt) {
setStatus("disconnected ..."); setStatus("disconnected!");
logined = false; logined = false;
user_list = new Array(); user_list = new Array();
ws = null; ws = null;
@ -158,6 +158,7 @@
message = "chat|" + msg + "|" + to_user; message = "chat|" + msg + "|" + to_user;
ws.send(message); ws.send(message);
document.getElementById(msgid).value = ""; document.getElementById(msgid).value = "";
document.all.msg.focus();
} }
function doClear(output) { function doClear(output) {
@ -176,7 +177,7 @@
<fieldset style="width:530px;"> <fieldset style="width:530px;">
<legend>WebSocket Chat</legend> <legend>WebSocket Chat</legend>
<p><span>Server url:</span> <p><span>Server url:</span>
<input id="url" type="text" value="ws://121.42.12.244:9001/" /> <input id="url" type="text" value="ws://127.0.0.1:9001/" />
<input type="button" id="connect" value="connect" onclick="OnConnect('user');" /> <input type="button" id="connect" value="connect" onclick="OnConnect('user');" />
</p> </p>
<p> <p>
@ -197,7 +198,7 @@
<div id="output" style="width:500px;height:300px; overflow:auto;"></div> <div id="output" style="width:500px;height:300px; overflow:auto;"></div>
<hr> <hr>
<div id="status" style="width:500px;height:25px; overflow:auto;" align = "right"> </div> <div id="status" style="width:500px;height:25px; overflow:auto;" align = "right"> </div>
<div><a href="https://github.com/zhengshuxin/acl/tree/master/app/wizard_demo/fiber_chat" target=_blank>源码</a></div> <div><a href="https://github.com/acl-dev/acl/tree/master/app/wizard_demo/fiber_chat" target=_blank>源码</a></div>
</fieldset> </fieldset>
</fieldset> </fieldset>
</div> </div>