- 浏览: 638037 次
- 性别:
- 来自: 成都
最新评论
-
zhima:
楼主还在升级开发吗?还是地址变动了
开源WebSocket服务器项目宝贝鱼CshBBrain V4.0.1 和 V2.0.2发布 -
linxingliang:
你好,我有一个iteye账号也被锁定了,你是通过什么方式联系管 ...
今天由于iteye账号被锁定所有博文不能访问,已解锁 -
独自空楼茉莉为谁而开:
解决啦,仿照你的广播发送写的代码 嘿嘿
开源WebSocket服务器项目 宝贝鱼(CshBBrain)版本发布 -
独自空楼茉莉为谁而开:
小弟有一个问题 我在本地开启两个页面访问后台,后台稍微修改下代 ...
开源WebSocket服务器项目 宝贝鱼(CshBBrain)版本发布 -
spqin:
...
百度地图API详解之地图标注
CshBBrain开发示例
1.服务器定时推送广播消息
服务器每隔10秒钟将服务器的当前时间所对应的毫秒数发送给客户端,客户端接收服务器发送的消息并在网页上显示出来。
1.1 我们创建一个线程来每隔10秒发送一个消息:
protected void startBroadMessage(){// 定时发送广播消息的线程
while(true){
try{
Response rs = new Response();// 创建一个响应消息
String msg = "current datetime of server current datetime of server current datetime of server current datetime of server current datetime of server current datetime of server: " + System.currentTimeMillis();
//System.out.println(msg);
rs.setBody(msg);// 给响应消息设置内容
MasterServer.addBroadMessage(rs);// 将要发送的消息广播出去
broadMessageThread.sleep(10000);// 线程睡眠10秒钟
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
1.2 启动服务器时,启动广播线程:
BroadThread.getInstance();// 创建广播线程
1.3 完整代码:
1.3.1 消息广播线程:
package com.jason.server.ws.biz;
import com.jason.server.Response;
import com.jason.server.MasterServer;
/**
* <li>类型名称:
* <li>说明:
* <li>创建人:CshBBrain;技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2012-8-24
* <li>修改人:
* <li>修改日期:
*/
public class BroadThread {
private static BroadThread broadThread= new BroadThread();
private Thread broadMessageThread;// 发送广播消息的线程
public static BroadThread getInstance(){
return broadThread;
}
private BroadThread(){
// 消息广播线程
Runnable writeDistributeRunner = new Runnable(){
public void run(){
try{
startBroadMessage();
}catch(Exception e){
e.printStackTrace();
}
}
};
this.broadMessageThread = new Thread(writeDistributeRunner);
this.broadMessageThread.setName("广播消息生成线程");
this.broadMessageThread.start();
}
protected void startBroadMessage(){
while(true){
try{
Response rs = new Response();
String msg = "current datetime of server current datetime of server current datetime of server current datetime of server current datetime of server current datetime of server: " + System.currentTimeMillis();
//System.out.println(msg);
rs.setBody(msg);
MasterServer.addBroadMessage(rs);
broadMessageThread.sleep(10000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
1.3.2 启动消息广播线程
package com.jason.server.ws;
import java.io.IOException;
import com.jason.server.MasterServer;
import com.jason.server.ws.biz.BroadThread;
/**
* <li>类型名称:
* <li>说明:websocket服务器入口类。
* <li>创建人:CshBBrain;技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2011-11-18
* <li>修改人:
* <li>修改日期:
*/
public class Server{
/**
* <li>方法名:main
* <li>@param args
* <li>返回类型:void
* <li>说明:WebSocket服务器,设置websocketdecoder,websocketProcess,websocketcoder给服务器
* <li>创建人:CshBBrain;技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2011-11-18
* <li>修改人:
* <li>修改日期:
*/
public static void main(String[] args){
try{
new MasterServer(new WebSocketCoder(), new WebSocketDecoder(), new Processer());
BroadThread.getInstance();
}catch(IOException e){
e.printStackTrace();
}
}
}
2.客户端与服务器相互发送消息
用户在网页上输入一个名字,提交到服务器,服务器回返回"Hello" + 提交的名字。
2.1 重写Service类中的 public Response service(Client sockector, HashMap<String, String> requestData)方法:
public Response service(Client sockector, HashMap<String, String> requestData){
if(requestData == null){
return null;
}
Response responseMessage = null;
try{
if(!MyStringUtil.isBlank(requestData.get(Constants.HANDSHAKE))){
responseMessage = Response.msgOnlyBody(requestData.get(Constants.FILED_MSG));
}else{
responseMessage = Response.msgOnlyBody("Hello," + requestData.get(Constants.FILED_MSG));// 将收到的人名前添加hello问候语
}
}catch(Exception e){
e.printStackTrace();
responseMessage = Response.msgOnlyBody("500处理失败了");
}
return responseMessage;
}
2.2 Service类完整代码:
/**
* <li>文件名:Service.java
* <li>说明:
* <li>创建人:CshBBrain;技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2011-11-27
* <li>修改人:
* <li>修改日期:
*/
package com.jason.server.ws.biz;
import java.util.HashMap;
import com.jason.server.Client;
import com.jason.server.Response;
import com.jason.util.MyStringUtil;
/**
* <li>类型名称:
* <li>说明:业务处理类
* <li>创建人:CshBBrain;技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2011-11-27
* <li>修改人:
* <li>修改日期:
*/
public class Service{
private static Service service = new Service();// 服务单实例;// 服务单实例
public static Service getInstance(){
return service;
}
private Service(){}
/**
*
* <li>方法名:service
* <li>@param requestData
* <li>@return
* <li>返回类型:ResponseMessage
* <li>说明:业务处理入口方法,对各种接口的请求进行处理
* <li>创建人:CshBBrain;技术博客:http://cshbbrain.iteye.com/
* <li>创建日期:2011-12-5
* <li>修改人:
* <li>修改日期:
*/
public Response service(Client sockector, HashMap<String, String> requestData){
if(requestData == null){
return null;
}
Response responseMessage = null;
try{
if(!MyStringUtil.isBlank(requestData.get(Constants.HANDSHAKE))){
responseMessage = Response.msgOnlyBody(requestData.get(Constants.FILED_MSG));
}else{
responseMessage = Response.msgOnlyBody("Hello," + requestData.get(Constants.FILED_MSG));
}
}catch(Exception e){
e.printStackTrace();
responseMessage = Response.msgOnlyBody("500处理失败了");
}
return responseMessage;
}
}
2.3 客户端页面代码:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="minimum-scale=1.0,maximum-scale=1,width=device-width,user-scalable=no" />
<title>首页</title>
<!--link rel="stylesheet" type="text/css" media="all" href="css/style.css" /-->
<link rel="stylesheet" type="text/css" media="all and (orientation:portrait)" href="css/style.css" />
<link rel="stylesheet" type="text/css" media="all and (orientation:landscape)" href="css/style_land.css"/>
<script src="js/scrollpic.js" type="text/javascript"></script>
</head>
<body>
<section class="nav">
<div class="subNav">
<div class="slipMarquee" id="stock">
</div>
<div>
<input type="text" id="txt" />
<button value="send" onclick="send();">send</button>
</div>
</div>
</section>
<script language="javascript" type="text/javascript">
window.WebSocket = window.WebSocket || window.MozWebSocket;
var socket = new WebSocket("ws://192.168.1.222:9090/echo"); // 注意这里的ip地址改为你自己的地址,创建sockect客户端
socket.onopen = function(){
//socket.send('i am client');
socket.onclose = function(e){// 关闭连接处理事件
alert('close the socket');
};
socket.onerror = function(msg){// 出错处理事件
alert('error:' + msg );
};
};
socket.onmessage = function(e) {// 收到消息处理事件,将收到的内容以红色显示在页面上
document.getElementById('stock').innerHTML = '<font color =red>' + event.data + '</font>';
};
function send(){// 发送输入的人名
socket.send(document.getElementById('txt').value);
}
</script>
</body>
</html>
评论
responseMessage = Response.msgOnlyBody(requestData.get(Constants.FILED_MSG));
}else{
responseMessage = Response.msgOnlyBody("Hello," + requestData.get(Constants.FILED_MSG));
}
这个方法返回的为什么在浏览器上是“Hello,null”呢?这个是public Response service(Client sockector, HashMap<String, String> requestData){}方法的,求解啊,我在send的数据在后台接收到了,但是在Response.msgOnlyBody("Hello," + requestData.get(Constants.FILED_MSG));返回的时候就是null了,求解啊
发表评论
-
jQuery那些有用的选择方式
2013-01-21 20:02 965摘自网络: $("[id^=percent]& ... -
基于宝贝鱼(CshBBrain)开发聊天类应用 群发消息的问题
2013-01-12 09:30 2356最近有网友 基于宝贝鱼(CshBBrain)开发聊天类应用 遇 ... -
JS字符串函数
2012-12-15 16:03 1298JS自带函数concat将两个或多个字符的文本组合起来,返回一 ... -
今天由于iteye账号被锁定所有博文不能访问,已解锁
2012-12-04 17:47 1847今天发布了开源WebSocket项目 宝贝鱼 CshBBr ... -
开源WebSocket服务器项目 宝贝鱼(CshBBrain)版本V4.0.2 和 V2.0.3发布
2012-12-04 10:23 2015宝贝鱼 CshBBrain V4.0.2 和 CshBB ... -
开源WebSocket服务器项目 宝贝鱼(CshBBrain) 前台js 框架CshBBrainJS V1.0.0发布(有图有真相)
2012-12-04 09:48 2899为了更好的支持开发基于Websocket的应用,开源Web ... -
基于开源WebSocket服务器宝贝鱼(CshBBrain)的应用横空出世
2012-12-04 10:24 2752开源WebSocket服务器 宝贝鱼(CshBBrain) ... -
HTML事件列表
2012-12-01 10:39 1007一下内容来自网络: 一般事件:onClick HTML ... -
JS编码解码函数
2012-11-23 16:16 1228js 对文字进行编码涉及3个函数:escape,enco ... -
开源WebSocket服务器项目宝贝鱼CshBBrain发布新版本,修复重大广播消息缺陷
2012-11-13 10:16 2682开源WebSocket服务器项目 ... -
开源WebSocket服务器项目宝贝鱼CshBBrain V4.0.1 和 V2.0.2发布
2012-11-13 09:57 2000开源WebSocket服务器项目宝贝鱼CshBBrain ... -
手机界面设计中9种常用的布局
2012-11-12 09:11 1972转自:http://reynold.cn/?p=422 ... -
Web开发者的最爱 5个超实用的HTML5 API
2012-11-09 13:19 1229转自:http://www.csdn.net/article/ ... -
JAVA AIO扫盲和入门
2012-11-05 13:33 9675开源WebSocket服务器 宝贝鱼(CshBBrain) ... -
国内首款基于AIO(异步IO)支持集群的高性能开源WebSocket服务器 宝贝鱼 CshBBrain V4.0 发布
2012-11-05 11:29 3562国内首款基于AIO的开源WebSocket服务器 宝贝鱼 ... -
开源WebSocket服务器项目CshBBrain 中文名:宝贝鱼
2012-11-03 14:38 1671有人叫我给 开源WebSocket服务器项目CshBBrain ... -
开源WebSocket服务器项目CshBBrain V2.0.1发布
2012-11-02 21:22 1570CshBBrain V2.0.1: ... -
高性能I/O设计模式Reactor和Proactor
2012-10-26 17:41 2312转自:http://hi.baidu.com/qhpgb ... -
Proactor和Reactor模式_继续并发系统设计的扫盲
2012-10-26 17:40 3814转自:http://www.cppblog.com/kevin ... -
reactor和proactor模式的比较
2012-10-26 17:38 1615转自:http://blog.163.com/zongyuan ...
相关推荐
hello world
OpenWrt之helloworld程序开发入门,里面包含helloworld和Makefile
输出HELLOWORLD然后输出Helloworld最后的最后的输出Helloworld
浙大ACm训练题目,也是浙大计算机研究生复试的上机训练题目代码
SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- Hello...
hello world prog for begginer
dpdk-helloworld for windows
c++编写hello world 简单的c++程序
打印HelloWorld ,在Visual Studio的开发环境当中,实现简单的操作。
一个简单的qt版helloworld程序
helloworld vbs 脚本hellohelloworld vbs 脚本world vbs 脚本helloworld vbs 脚本helloworld vbs 脚本helloworld vbs 脚本
• 在命令行打印 hello world! 字符 //mkdir nodejs-demo //cd nodejs-demo // https://sample.wangding.in/nodejs/tern-project //mv tern-project .tern-project //mkdir 01-introduction //touch 01-hello-world....
Hello World C源码 Hello World C源码 Hello World C源码 Hello World C源码 Hello World C源码
C#第一个程序,helloworld using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace helloworld { class helloworld { [STAThread] static void Main(string[] args)...
abc 联系代码hello world hello worldabc 联系代码hello world hello world
深入浅出hello world。从一个简单的hello,world程序开始,经过预处理、编译、汇编、链接、加载各个步骤详细介绍程序从编辑到执行的过程。深入的讲解了linux下程序的编译过程,并且讲解了linux kernel系统调用的相关...
open cascade hello world open cascade hello world open cascade hello world open cascade hello world open cascade hello world open cascade hello world open cascade hello world open cascade hello world
Spring4 HelloWorld初学Spring,java bean通过applicationContext.xml配置SpringIoC容器生成。
最小的C语言Hello, world程序
just helloworld for fucking points