String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
">
My JSP 'index.jsp' starting pageString message_to = request.getParameter( "message_to" );
String message_me = request.getParameter( "message_me" );
request.setAttribute( "message_to" , message_to );
request.setAttribute( "message_me" , message_me );
%>
$(function ()
{
window.onbeforeunload = onbeforeunload_handler;
window.onunload = onunload_handler;
function onbeforeunload_handler(){
//ws.close();
return warning;
}
function onunload_handler()
{
//alert(1);
ws = null;
}
});
var message_to = "${message_to}";
var message_me = "${message_me}";
//var ws = new WebSocket("ws://192.168.202.56:8080/websocket_msg/serverSocket?message_to="+message_to+"&message_me="+message_me);
var url = "websocket_msg/serverSocket?message_to="+message_to+"&message_me="+message_me;
var ws = new $.websocket({
protocol : "websocket_msg/serverSocket?message_to="+message_to+"&message_me="+message_me,
domain : "192.168.1.120",
port : "8080",
onOpen:function(event){
showMessage("已成功登录");
},
onError:function(event){
alert("error:"+ event)
},
onMessage:function(result){
receiveMessage(result);
},
onClose:function(event){
ws = null;
}
});
function send(){
if(!ws){
alert("已经断开聊天室");
return;
}
var msg=$.trim($("#msg").val());
if(msg==""){return;}
ws.send(msg);
$("#messageInput").val("").focus();;
}
function receiveMessage(result){
showMessage(result);
}
function showMessage(msg){
document.getElementById("chatlog").textContent += msg + "\n";
}
Send!
End