netty杜绝不合法数据包_搭建百万连接服务,使用netty完成websocke的推送

本文详细介绍了如何使用Netty构建WebSocket服务器,实现百万连接。讲解了WebSocket协议的原理和实现,展示了Java代码示例。同时探讨了Netty如何在Linux环境下配置以支持大量连接,包括端口复用、系统参数调整等关键点。
摘要由CSDN通过智能技术生成
(一)使用websocket
  • ① 介绍

webSocket协议是基于TCP的一种新的网络协议。他的出现实现了网络和浏览器全双工通信,允许服务器主动发送信息给客户端。客户端 给 服务器发消息是半双工,服务器给客户端也发送消息就是全双工。

多客户端多语言多浏览器支持:浏览器,php,Java,ruby,nginx,python,Tomcat,erlang,.net等等。

a3afdc8b0abb1e10d411e7360cd8af09.png

  • ② 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 类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值