我现在想做一个网页聊天的系统,我用的是pusulet框架,下载的别人的例子改的,也算是实现了,可是聊天时一会可以用一会发不出去信息了,控制台不定时的提示-----??? removed ,我试着找找他出现removed客户的联接 的 规律,找得我头都快爆了,我怀疑我用它的方法不对,请会用的帮个忙,给俺说说怎么用,
下面我就把我做的给大家讲一下,有兴趣的朋友看看我的代码:web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>pushlet</servlet-name>
<servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>PustServlet</servlet-name>
<servlet-class>com.test.pushlet.PustletEvent</servlet-class>
</servlet>
<!-- Define the Servlet Mappings. --> <!-- The pushlet -->
<servlet-mapping>
<servlet-name>pushlet</servlet-name>
<url-pattern>/pushlet.srv</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PustServlet</servlet-name>
<url-pattern>/PustServlet</url-pattern>
</servlet-mapping>
</web-app>
下面我就把我做的给大家讲一下,有兴趣的朋友看看我的代码:web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>pushlet</servlet-name>
<servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>PustServlet</servlet-name>
<servlet-class>com.test.pushlet.PustletEvent</servlet-class>
</servlet>
<!-- Define the Servlet Mappings. --> <!-- The pushlet -->
<servlet-mapping>
<servlet-name>pushlet</servlet-name>
<url-pattern>/pushlet.srv</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PustServlet</servlet-name>
<url-pattern>/PustServlet</url-pattern>
</servlet-mapping>
</web-app>
解决方案 »
- 关于flex项目中,java后台如何向前台传递大数据的问题
- JDK1.7部分新特性用不了咋回事?
- FTP 425 Cannot open data connection
- linux上验证码显示不出来
- 怎样建立自己的代码库
- 来了就给分~~~~~~有哪位达人用了JAVA实现的规则引擎和工作流引擎
- 求tomcat整合hibernate的详细步骤
- ssh中事物问题
- unitils作单元测试 时用@SpringApplicationContext加载配置文件为nll
- 有没有免费的jsp debug和javascript debug的eclipse插件?告知名称,下载地址马上给100分!
- java 数据库 读取数据问题
- 关于EL表达式异常:javax.el.PropertyNotFoundException
sources.properties :
source2=com.test.pushlet.PustletEventpushlet.properties :较多,等会我发个图片吧
package com.test.pushlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.test.frame.MainFrame;import nl.justobjects.pushlet.core.Dispatcher;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventSource;public class PustletEvent extends HttpServlet implements EventSource {
MainFrame mframe = null;
private Event event = null; public void activate() {
System.out.println("活动"); } public void passivate() {
System.out.println("---"); } public void stop() {
System.out.println("stop"); } // 方法随便定义 public void pusuletEvent(String msg, String session) {
if (event == null) {
// 监听主题
// System.out.print("\n======"+msg+"\n");
// event = Event.createDataEvent("/"+session);
event = Event.createDataEvent("/caht2");
}
event.setField("msg", msg);
Dispatcher.getInstance().multicast(event); } public void pusuletEvent2(String msg, String sessionid) {
if (event == null) {
// 监听主题
event = Event.createDataEvent("/chat");
}
event.setField("msg", msg);
event.setField("sessionid", sessionid);
Dispatcher.getInstance().multicast(event); } /*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/ public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String str = request.getParameter("msg");
String sessionid = request.getParameter("sessionid");
String session = request.getParameter("session");
if (null == sessionid) {
this.pusuletEvent(str, session);
} else { this.pusuletEvent2(str, sessionid);
}
System.out.println("jjjjjjjjjjjjjjjjj");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}}
下面这个是界面,两个:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}.style2 {
font-size: 12px
}.style11 {
font-size: 13px;
color: #000000;
}a:link {
text-decoration: none;
}a:visited {
text-decoration: none;
}a:hover {
text-decoration: underline;
}a:active {
text-decoration: none;
}.style12 {
color: #666666
}.style13 {
font-size: 13px;
color: #666666;
}
-->
</style>
<script type="text/javascript" src="lib/js-pushlet-client.js"></script>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
//定义变量区
var sessionid=null;
var nSbKey=1;
var keysub=1;//定义方法区
// Initialization
//注册主题
function init() {
p_join_listen('/chat');
} // Event Callback: display all events
//接收服务器推来的信息
function onData(event) {
// p_debug(flag, "pushlet-app", 'event received subject=' + event.getSubject() );
//document.eventDisplay.event.value = event.toString();
var str = event.get("msg");
sessionid=event.get("sessionid");
//alert(str);
//alert(sessionid);
showMsg(str);
}
function showMsg(msg){
var nowTime=new Date().toLocaleString();
document.getElementById("writeStatus").innerHTML=""
parent.olEditor.document.body.innerHTML=parent.olEditor.document.body.innerHTML+"<span style='color: #3366CC;font-size: 13px;'>"+nowTime+" 说:</span><br>"+msg + "<br>";
window.focus();
scrolling();//调用滚动消息的方法
}
///==========================================================================聊天窗上边滚动条始终在下面
//var loop
function scrolling() {
window.olEditor.scroll(0,60000)//+window.olEditor.document.body.scrollTop+move) ;
}
//当键盘按下Enter时触发的事件
function fnKeypress() {
if(keysub==1){
if(nSbKey==1){
if(event.ctrlKey && window.event.keyCode==13){
return sendSub();//发送消息
}
}
else{
if(window.event.keyCode==13){
event.keyCode = 0;
event.returnValue = false;
return sendSub()
}
}
}
}
//改变是否用 enter键 发送消息
function changeSbKey(){
if (document.getElementById("sb").checked){
nSbKey=2
}else{
nSbKey=1
}
}//发送消息
function sendSub(){
var showdData=document.getElementById("wrEditor").value;
$.get("PustServlet?session="+sessionid+"&msg="+showdData, null,
function(data){ //回传的数据类型
document.getElementById("wrEditor").value="";
showMsg(showdData);
});
}
</script>
</head> <body onload="init()">
<div id="div1"></div>
<script type="text/javascript">p_embed()</script> <table width="554" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="554" height="425" align="center" valign="top"
background="images/chatWinBg.gif">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="65%">
</td>
<td width="33%" rowspan="6" align="right" valign="top"> <table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="39">
</td>
</tr>
<tr>
<td height="109" align="center">
<img src="images/thumb_20061011110565047.jpg" width="64"
height="89">
</td>
</tr>
<tr>
<td height="37">
</td>
</tr>
<tr>
<td height="236" align="right" valign="top">
<table width="96%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30" class="style11 style12"></td>
</tr>
<tr>
<td height="30" class="style13"></td>
</tr>
<tr>
<td height="30" class="style13">
<span class="style11 style12"></span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="2%" rowspan="6" align="center" valign="top">
</td>
</tr>
<tr>
<td height="30" valign="top">
<table width="100%" height="28" cellpadding="0" cellspacing="0"> <!--edit by zhz 更改为显示对方的名字-->
<tr>
<td width="6%" height="26">
</td>
<td width="80%" align="left"
style="text-decoration: underline; color: gray;"></td>
<td width="14%" height="26">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="215" align="right" valign="top">
<table width="95%" height="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td height="168" align="center" valign="bottom">
<iframe id="olEditor" width="90%" height="100%"
SCROLLING="auto" frameborder="0" marginHeight="0"></iframe>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="20" valign="top">
<table width="98%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="7%" height="20" valign="top">
</td>
<td valign="middle">
<span id="ChatWithWho" class="style2"> <script
language="javascript">
ChatWithWho.innerHTML="与 交谈中";
</script> <span id="note" title="将Enter设置为发送快捷键" class="style2"
style="display: "> <input name="sb" type="checkbox"
id="sb" onClick="changeSbKey()" value="1">
按Enter发送 </span> </span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="72" align="right" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="6%" height="74">
</td><td width="6%" height="74">
</td><td width="6%" height="74">
</td> <td width="78%" align="left" valign="top">
<textarea name="wrEditor" cols="38" rows="4"
STYLE="border: 0px; overflow-y: auto" id="wrEditor"
onKeyDown="fnKeypress()"></textarea>
</td>
<td width="14%" align="center">
<a href="javascript: sendSub()" id="sdb"><img
src="images/chatWinButton.jpg" width="52" height="44"
border="0"> </a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="22" valign="top" id="writeStatus">
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
对不起大家呀,
这个才是其中一个聊天窗口,
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}.style2 {
font-size: 12px
}.style11 {
font-size: 13px;
color: #000000;
}a:link {
text-decoration: none;
}a:visited {
text-decoration: none;
}a:hover {
text-decoration: underline;
}a:active {
text-decoration: none;
}.style12 {
color: #666666
}.style13 {
font-size: 13px;
color: #666666;
}
-->
</style>
<script type="text/javascript" src="lib/js-pushlet-client.js"></script>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
//定义变量区
var nSbKey=1;
var keysub=1;//定义方法区
// Initialization
//注册主题
function init() {
//p_join_listen("/"+"<%=session.getId()%>");
p_join_listen("/caht2");
} // Event Callback: display all events
//接收服务器推来的信息
function onData(event) {
// p_debug(flag, "pushlet-app", 'event received subject=' + event.getSubject() );
//document.eventDisplay.event.value = event.toString();
var str = event.get("msg");
//alert(str);
showMsg(str);
}
function showMsg(msg){
var nowTime=new Date().toLocaleString();
document.getElementById("writeStatus").innerHTML=""
parent.olEditor.document.body.innerHTML=parent.olEditor.document.body.innerHTML+"<span style='color: #3366CC;font-size: 13px;'>"+nowTime+" 说:</span><br>"+msg + "<br>";
window.focus();
scrolling();//调用滚动消息的方法
}
///==========================================================================聊天窗上边滚动条始终在下面
//var loop
function scrolling() {
window.olEditor.scroll(0,60000)//+window.olEditor.document.body.scrollTop+move) ;
}
//当键盘按下Enter时触发的事件
function fnKeypress() {
if(keysub==1){
if(nSbKey==1){
if(event.ctrlKey && window.event.keyCode==13){
return sendSub();//发送消息
}
}
else{
if(window.event.keyCode==13){
event.keyCode = 0;
event.returnValue = false;
return sendSub()
}
}
}
}
//改变是否用 enter键 发送消息
function changeSbKey(){
if (document.getElementById("sb").checked){
nSbKey=2
}else{
nSbKey=1
}
}//发送消息
function sendSub(){
var showdData=document.getElementById("wrEditor").value;
$.get("PustServlet?sessionid=<%=session.getId()%>&msg="+showdData, null,
function(data){ //回传的数据类型
document.getElementById("wrEditor").value="";
showMsg(showdData);
});
}
</script>
</head> <body onload="init()">
<div id="div1"></div>
<script type="text/javascript">p_embed()</script> <table width="554" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="554" height="425" align="center" valign="top"
background="images/chatWinBg.gif">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="65%">
</td>
<td width="33%" rowspan="6" align="right" valign="top"> <table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="39">
</td>
</tr>
<tr>
<td height="109" align="center">
<img src="images/thumb_20061011110565047.jpg" width="64"
height="89">
</td>
</tr>
<tr>
<td height="37">
</td>
</tr>
<tr>
<td height="236" align="right" valign="top">
<table width="96%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30" class="style11 style12"></td>
</tr>
<tr>
<td height="30" class="style13"></td>
</tr>
<tr>
<td height="30" class="style13">
<span class="style11 style12"></span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="2%" rowspan="6" align="center" valign="top">
</td>
</tr>
<tr>
<td height="30" valign="top">
<table width="100%" height="28" cellpadding="0" cellspacing="0"> <!--edit by zhz 更改为显示对方的名字-->
<tr>
<td width="6%" height="26">
</td>
<td width="80%" align="left"
style="text-decoration: underline; color: gray;"></td>
<td width="14%" height="26">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="215" align="right" valign="top">
<table width="95%" height="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td height="168" align="center" valign="bottom">
<iframe id="olEditor" width="90%" height="100%"
SCROLLING="auto" frameborder="0" marginHeight="0"></iframe>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="20" valign="top">
<table width="98%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="7%" height="20" valign="top">
</td>
<td valign="middle">
<span id="ChatWithWho" class="style2"> <script
language="javascript">
ChatWithWho.innerHTML="与 交谈中";
</script> <span id="note" title="将Enter设置为发送快捷键" class="style2"
style="display: "> <input name="sb" type="checkbox"
id="sb" onClick="changeSbKey()" value="1">
按Enter发送 </span> </span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="72" align="right" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="6%" height="74">
</td><td width="6%" height="74">
</td><td width="6%" height="74">
</td> <td width="78%" align="left" valign="top">
<textarea name="wrEditor" cols="38" rows="4"
STYLE="border: 0px; overflow-y: auto" id="wrEditor"
onKeyDown="fnKeypress()"></textarea>
</td>
<td width="14%" align="center">
<a href="javascript: sendSub()" id="sdb"><img
src="images/chatWinButton.jpg" width="52" height="44"
border="0"> </a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="22" valign="top" id="writeStatus">
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>