(一)使用websocket
① 介绍
webSocket协议是基于TCP的一种新的网络协议。他的出现实现了网络和浏览器全双工通信,允许服务器主动发送信息给客户端。客户端 给 服务器发消息是半双工,服务器给客户端也发送消息就是全双工。
多客户端多语言多浏览器支持:浏览器,php,Java,ruby,nginx,python,Tomcat,erlang,.net等等。
② websocket实现
服务端和客户端交流,通过的是websocket这种协议,内部传输的协议,通过websocket这种方式和普通的socket没有什么区别,唯一一点就是协议不同。
③ websocket示例
绑定9001,这是浏览器js方式完成的,可以发送消息和接收消息,生成一个随机数userId,在设计这个功能的时候,考虑到消息推送是否需要先登录。登录过跳转到登录成功,成功之后拿到userId,在建立websocket连接的时候就可以携带userId,最终知道是那个用户。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Web Socket Testtitle>
head>
<body>
<script type="text/javascript">var socket;if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket;
}if (window.WebSocket) {
// 随机数var random = Math.floor(Math.random()*(10000 - 10 +1) + 10)
socket = new WebSocket("ws://127.0.0.1:9001/websocket?userId=" + random);
socket.onmessage = function(event) {
var ta = document.getElementById('responseText');
ta.value = ta.value + '\n' + event.data
};
socket.onopen = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket opened!";
};
socket.onclose = function(event) {
var ta = document.getElementById('responseText');
ta.value = ta.value + "Web Socket closed";
};
} else {
alert("Your browser does not support Web Socket.");
}function send(message) {
if (!window.WebSocket) { return; }if (socket.readyState == WebSocket.OPEN) {
socket.send(message);
} else {
alert("The socket is not open.");
}
}script>
<form onsubmit="return false;">
<input type="text" name="message" value="Hello, World!" />
<input type="button" value="Send Web Socket Data" onclick="send(this.form.message.value)" />
<h3>Outputh3>
<textarea id="responseText" style="width:500px;height:300px;">textarea>
form>
body>
html>
java代码示例server端
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public final class WebSocketServer {
static int PORT = 9000;
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, true)
.childHandler(new WebSocketServerInitializer())
.childOption(ChannelOption.SO_REUSEADDR, true);
b.bind(++PORT).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if ("true".equals(System.getProperty("netease.debug")))
System.out.println("端口绑定完成:" + future.channel().localAddress());
}
});
// 端口绑定完成,启动消息随机推送(测试)
TestCenter.startTest();
System.in.read();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
server端的 channel类。绑定完成相关的业务逻辑处理,
1.解析http协议编解码
2.最大的解析数据包拦截
3.自己的业务处理,得到msg对象,变成了一个request的对象。
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
/**
*/
public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
// 职责链, 数据处理流程
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec()); //
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WebSocketServerHandler());
pipeline.addLast(new NewConnectHandler());
}
}
TestCenter 类