问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

itcp wed components 是什么软件

发布网友 发布时间:2022-05-07 05:27

我来回答

2个回答

热心网友 时间:2023-10-16 23:08

netty v3.9.4
websocket连接建立前,客户端需要与服务器进行握手(http协议) 确认websocket连接,也就是说在处理websocket请求前,必需要处理一些http请求。
websocket到现在为止,已经有多个版本,netty有相应的对应类,这部分处理一般不需要人工干预。

如果运行正常的话,会在页面的文本框中显示1-20记数。
可以通过firefox或chrome的开发人员工具,显看浏览器与服务器的交互。

主要是HttpServerChannelHandler2,加了些注释和少量debug代码。
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

package org.sl.demo.httpserver1;

import java.util.List;
import java.util.Map;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;

public class HttpServerChannelHandler2 extends SimpleChannelHandler{
public static boolean debug = true;

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
Channel ch = e.getChannel();
Object msg = e.getMessage();

if(debug){
System.out.println("---------------");
System.out.println("message: "+msg.getClass());
}
//虽然是websocket,但在建立websocket连接前,先进行http握手,所以,这时也要处理http请求
//在http握手完成后,才是websocket下的通信
if(msg instanceof HttpRequest){
processHttpRequest(ch, (HttpRequest)msg);
}else if(msg instanceof WebSocketFrame){
processWebsocketRequest(ch,(WebSocketFrame)msg);
}else{
//未处理的请求类型
}
}

//这个方法:
//1.完成websocket前的http握手
//2.屏蔽掉非websocket握手请求
void processHttpRequest(Channel channel,HttpRequest request){
HttpHeaders headers = request.headers();
if(debug){
List<Map.Entry<String,String>> ls = headers.entries();
for(Map.Entry<String,String> i: ls){
System.out.println("header "+i.getKey()+":"+i.getValue());
}
}

//屏蔽掉非websocket握手请求
//只接受http GET和headers['Upgrade']为'websocket'的http请求
if(!HttpMethod.GET.equals(request.getMethod())
|| !"websocket".equalsIgnoreCase(headers.get("Upgrade"))){
DefaultHttpResponse resp = new DefaultHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.BAD_REQUEST);
channel.write(resp);
channel.close();
return;
}

WebSocketServerHandshakerFactory wsShakerFactory = new WebSocketServerHandshakerFactory(
"ws://"+request.headers().get(HttpHeaders.Names.HOST),
null,false );
WebSocketServerHandshaker wsShakerHandler = wsShakerFactory.newHandshaker(request);
if(null==wsShakerHandler){
//无法处理的websocket版本
wsShakerFactory.sendUnsupportedWebSocketVersionResponse(channel);
}else{
//向客户端发送websocket握手,完成握手
//客户端收到的状态是101 sitching protocol
wsShakerHandler.handshake(channel, request);
}
}

//websocket通信
void processWebsocketRequest(Channel channel, WebSocketFrame request){
if(request instanceof CloseWebSocketFrame){
channel.close();
}else if(request instanceof PingWebSocketFrame){
channel.write(new PongWebSocketFrame(request.getBinaryData()));
}else if(request instanceof TextWebSocketFrame){
//这个地方 可以根据需求,加上一些业务逻辑
TextWebSocketFrame txtReq = (TextWebSocketFrame) request;
if(debug){ System.out.println("txtReq:"+txtReq.getText());}

//向ws客户端发送多个响应
for(int i=1; i<=20; i++){
channel.write(new TextWebSocketFrame(""+i));
try{Thread.sleep(300);}catch(Exception ex){}
}
}else{
//WebSocketFrame还有一些
}
}
}

其他类跟网上的差不多:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

package org.sl.demo.httpserver1;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

public class HttpServer implements Runnable{
int port = 80;

public HttpServer(int port){
this.port = port;
}

@Override
public void run() {
ServerBootstrap b = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
b.setPipelineFactory(new HttpServerChannelPipelineFactory());
b.setOption("child.tcpNoDelay", true);
b.setOption("child.keepAlive", true);
b.bind(new InetSocketAddress(port));
}

public static void main(String[] args){
new HttpServer(80).run();
}
}

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

package org.sl.demo.httpserver1;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket00FrameDecoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;

public class HttpServerChannelPipelineFactory implements ChannelPipelineFactory {

@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline cp = Channels.pipeline();

cp.addLast("decoder", new HttpRequestDecoder());
// cp.addLast("decoder", new WebSocket00FrameDecoder());
cp.addLast("encoder", new HttpResponseEncoder());
// cp.addLast("downhandler", new HttpServerDownstreamHandler());
// cp.addLast("uphandler", new HttpServerUpstreamHandler());
cp.addLast("handler", new HttpServerChannelHandler2());

return cp;
}

}

测试页面:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

<html>
<head>
<script >
function connect1(){
alert('connect1');

var ta = document.getElementById('responseText');
var socket = new WebSocket('ws://127.0.0.1/websocket');
if (window.WebSocket) {

}else{
alert('Your browser does not support Web Socket.');
return;
}
socket.onopen = function(event) {
ta.value = "Web Socket opened!";
}

socket.onmessage = function(event) {
ta.value = event.data;
};

socket.onclose = function(event) {
ta.value = "Web Socket closed";
};
}

function connect() {
alert('connect');
var socket;
if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket;
}

if (window.WebSocket) {
socket = new WebSocket("ws://127.0.0.1/websocket");
socket.onmessage = function(event) {
var ta = document.getElementById('responseText');
ta.value = event.data;
};

socket.onopen = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket opened!";
socket.send('hello');
};

socket.onclose = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket closed";
};

socket.onerror = function(event){
};
} else {
alert("Your browser does not support Web Socket.");
}
}
</script>
</head>

<body>
<input type="button" onclick="connect1()" value="ws connect1"/> <br/><br/>
<input type="button" onclick="connect()" value="ws connect"/> <br/><br/>
<input id="responseText" type="text" value="123456"/>
</body>
</html>

热心网友 时间:2023-10-16 23:08

netty v3.9.4
websocket连接建立前,客户端需要与服务器进行握手(http协议) 确认websocket连接,也就是说在处理websocket请求前,必需要处理一些http请求。
websocket到现在为止,已经有多个版本,netty有相应的对应类,这部分处理一般不需要人工干预。

如果运行正常的话,会在页面的文本框中显示1-20记数。
可以通过firefox或chrome的开发人员工具,显看浏览器与服务器的交互。

主要是HttpServerChannelHandler2,加了些注释和少量debug代码。
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

package org.sl.demo.httpserver1;

import java.util.List;
import java.util.Map;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;

public class HttpServerChannelHandler2 extends SimpleChannelHandler{
public static boolean debug = true;

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
Channel ch = e.getChannel();
Object msg = e.getMessage();

if(debug){
System.out.println("---------------");
System.out.println("message: "+msg.getClass());
}
//虽然是websocket,但在建立websocket连接前,先进行http握手,所以,这时也要处理http请求
//在http握手完成后,才是websocket下的通信
if(msg instanceof HttpRequest){
processHttpRequest(ch, (HttpRequest)msg);
}else if(msg instanceof WebSocketFrame){
processWebsocketRequest(ch,(WebSocketFrame)msg);
}else{
//未处理的请求类型
}
}

//这个方法:
//1.完成websocket前的http握手
//2.屏蔽掉非websocket握手请求
void processHttpRequest(Channel channel,HttpRequest request){
HttpHeaders headers = request.headers();
if(debug){
List<Map.Entry<String,String>> ls = headers.entries();
for(Map.Entry<String,String> i: ls){
System.out.println("header "+i.getKey()+":"+i.getValue());
}
}

//屏蔽掉非websocket握手请求
//只接受http GET和headers['Upgrade']为'websocket'的http请求
if(!HttpMethod.GET.equals(request.getMethod())
|| !"websocket".equalsIgnoreCase(headers.get("Upgrade"))){
DefaultHttpResponse resp = new DefaultHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.BAD_REQUEST);
channel.write(resp);
channel.close();
return;
}

WebSocketServerHandshakerFactory wsShakerFactory = new WebSocketServerHandshakerFactory(
"ws://"+request.headers().get(HttpHeaders.Names.HOST),
null,false );
WebSocketServerHandshaker wsShakerHandler = wsShakerFactory.newHandshaker(request);
if(null==wsShakerHandler){
//无法处理的websocket版本
wsShakerFactory.sendUnsupportedWebSocketVersionResponse(channel);
}else{
//向客户端发送websocket握手,完成握手
//客户端收到的状态是101 sitching protocol
wsShakerHandler.handshake(channel, request);
}
}

//websocket通信
void processWebsocketRequest(Channel channel, WebSocketFrame request){
if(request instanceof CloseWebSocketFrame){
channel.close();
}else if(request instanceof PingWebSocketFrame){
channel.write(new PongWebSocketFrame(request.getBinaryData()));
}else if(request instanceof TextWebSocketFrame){
//这个地方 可以根据需求,加上一些业务逻辑
TextWebSocketFrame txtReq = (TextWebSocketFrame) request;
if(debug){ System.out.println("txtReq:"+txtReq.getText());}

//向ws客户端发送多个响应
for(int i=1; i<=20; i++){
channel.write(new TextWebSocketFrame(""+i));
try{Thread.sleep(300);}catch(Exception ex){}
}
}else{
//WebSocketFrame还有一些
}
}
}

其他类跟网上的差不多:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

package org.sl.demo.httpserver1;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

public class HttpServer implements Runnable{
int port = 80;

public HttpServer(int port){
this.port = port;
}

@Override
public void run() {
ServerBootstrap b = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
b.setPipelineFactory(new HttpServerChannelPipelineFactory());
b.setOption("child.tcpNoDelay", true);
b.setOption("child.keepAlive", true);
b.bind(new InetSocketAddress(port));
}

public static void main(String[] args){
new HttpServer(80).run();
}
}

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

package org.sl.demo.httpserver1;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket00FrameDecoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;

public class HttpServerChannelPipelineFactory implements ChannelPipelineFactory {

@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline cp = Channels.pipeline();

cp.addLast("decoder", new HttpRequestDecoder());
// cp.addLast("decoder", new WebSocket00FrameDecoder());
cp.addLast("encoder", new HttpResponseEncoder());
// cp.addLast("downhandler", new HttpServerDownstreamHandler());
// cp.addLast("uphandler", new HttpServerUpstreamHandler());
cp.addLast("handler", new HttpServerChannelHandler2());

return cp;
}

}

测试页面:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

<html>
<head>
<script >
function connect1(){
alert('connect1');

var ta = document.getElementById('responseText');
var socket = new WebSocket('ws://127.0.0.1/websocket');
if (window.WebSocket) {

}else{
alert('Your browser does not support Web Socket.');
return;
}
socket.onopen = function(event) {
ta.value = "Web Socket opened!";
}

socket.onmessage = function(event) {
ta.value = event.data;
};

socket.onclose = function(event) {
ta.value = "Web Socket closed";
};
}

function connect() {
alert('connect');
var socket;
if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket;
}

if (window.WebSocket) {
socket = new WebSocket("ws://127.0.0.1/websocket");
socket.onmessage = function(event) {
var ta = document.getElementById('responseText');
ta.value = event.data;
};

socket.onopen = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket opened!";
socket.send('hello');
};

socket.onclose = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket closed";
};

socket.onerror = function(event){
};
} else {
alert("Your browser does not support Web Socket.");
}
}
</script>
</head>

<body>
<input type="button" onclick="connect1()" value="ws connect1"/> <br/><br/>
<input type="button" onclick="connect()" value="ws connect"/> <br/><br/>
<input id="responseText" type="text" value="123456"/>
</body>
</html>

热心网友 时间:2023-10-16 23:09

海康威视的视频监控插件

热心网友 时间:2023-10-16 23:09

海康威视的视频监控插件
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
怎样备份iPhone的照片? 奇迹的战士怎样加点? 奇迹私服战士8W点的怎么加点PK厉害 房屋装修建材的选购家装建材种类有哪些 家装建材都包含什么 家装应该买什么东西 劓刑简介 电信手机停机保号怎么办理? 定胆定胆方法 3d定胆方法有哪些? websoc卸载呀?用电脑带360卸不了,气死了 你好,请问我刚刚买戴尔的电脑,不小心把websoc的软件卸载了,我上网查... 戴尔电脑没有这个websoc的软件对电脑有影响吗? 刚刚买的戴尔电脑,不小心卸载了websoc,这是什么软件啊,在网上下载不... 怎么知道对方是否被封? 怎么知道自己的被封了 怎样查看被封多久? 包皮垢多,恶臭,经常痒,用了红霉素软膏还有达克宁,都不见效 为什么我包皮剥开里面很臭 16岁*包皮过长有红邹还会发阵阵恶臭需要及时割吗? 等1年后在割可以卜 急 假性包皮过长,有恶臭,在性生活中对女性有伤害吗 AI概念股票有哪些的最新相关信息 为什么龟头恶臭? 人工智能板块股票有哪些? 我的包皮里有白色的有恶臭的物体,不知道是怎么回事?每次洗后还是有。请问是怎么回事? 包皮过长陪伴了我二十几年,最近天气炎热,*出汗后,*总是发炎,恶臭,疼痛,现在想割掉。我在乐清 包皮间臭怎么吧 老人与海读书报告中文1000 演讲搞寒假读书300字老人与海 急需一篇《老人与海》读书报告,帮帮忙 怎样查看被封多久 精卫填海续如何结局? 谁知道&lt;精卫填海&gt;的大结局? 精卫填海大结局改写 报名教师资格证没有户口本怎么办 河南教师资格证报名条件?除了毕业证和普通话证还要什么证件?不在户口所在地可以报名吗?报名需要户口本吗 教师资格证面试现场报名 可以带户口本复印件么? 凡尔赛一号性价比高吗?靠谱吗? 面粉是用什么来做的 凡尔赛1号直接买吗?性价比高吗怎么样?适合哪些人买? 教师资格证报名可以用户口本吗 教师资*报名需要提供报名费和户口本,难道有地域*吗?户口是哪里的就得在哪儿报名吗? 凡尔赛一号性价比高吗?注意哪些问题? 报名教师资格证认证必须是户口页原件吗 报考教师资格证去教育局交钱要带户口本吗 金华教师证现场报名一定需要带户口本吗 凡尔赛1号直接买吗?性价比高吗怎么样?靠谱吗? 教师资格证报名需要户口本本人页,还需要户口本户主页吗 报考教师资格证现场确认没有身份证有户口本行吗 考幼师资格证必须要户主户口本吗