python与html5搭建聊天室_基于Html5 websocket和Python的在线聊天室(客户端)

WebSocket

html,body{font:normal 0.9em arial,helvetica;}

#log {width:440px; height:200px; border:1px solid #7F9DB9; overflow:auto;}

#msg {width:330px;}

var socket;

function init(){

var host = "ws://192.168.20.31:1234/";

try{

socket = new WebSocket(host);

socket.onopen = function(msg){ ; };

socket.onmessage = function(msg){ log(msg.data); };

socket.onclose = function(msg){ log("Lose Connection!"); };

}

catch(ex){ log(ex); }

$("msg").focus();

}

function send(){

var txt,msg;

txt = $("msg");

msg = txt.value;

if(!msg){ alert("Message can not be empty"); return; }

txt.value="";

txt.focus();

try{ socket.send(msg); } catch(ex){ log(ex); }

}

window.οnbefοreunlοad=function(){

try{

socket.send('quit');

socket.close();

socket=null;

}

catch(ex){

log(ex);

}

};

function $(id){ return document.getElementById(id); }

function log(msg){ $("log").innerHTML+="
"+msg; }

function onkey(event){ if(event.keyCode==13){ send(); } }

WebSocket

发送

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值