如何让java websocket服务端 支持wss
展开全部
客户端:
client
1
2
3
4
5
6
7
<!DOCTYPE html>
<html>
<body>
<h1>WebSocket</h1>
<script src="client.js"></script>
</body>
</html>
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var ws = new WebSocket("ws://127.0.0.1:8080/");
ws.onopen = function() {
alert("Opened");
ws.send("I'm client");
};
ws.onmessage = function (evt) {
alert(evt.data);
};
ws.onclose = function() {
alert("Closed");
};
ws.onerror = function(err) {
alert("Error: " + err);
};
服务端可以用各种语言去实现,Java可以用Jetty,C#可以用SuperSocket,这里我举例用Node.js,创建server.js:
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
});
ws.send('something');
});
运行方式:
1
2
npm install ws
node server.js
client
1
2
3
4
5
6
7
<!DOCTYPE html>
<html>
<body>
<h1>WebSocket</h1>
<script src="client.js"></script>
</body>
</html>
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var ws = new WebSocket("ws://127.0.0.1:8080/");
ws.onopen = function() {
alert("Opened");
ws.send("I'm client");
};
ws.onmessage = function (evt) {
alert(evt.data);
};
ws.onclose = function() {
alert("Closed");
};
ws.onerror = function(err) {
alert("Error: " + err);
};
服务端可以用各种语言去实现,Java可以用Jetty,C#可以用SuperSocket,这里我举例用Node.js,创建server.js:
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
});
ws.send('something');
});
运行方式:
1
2
npm install ws
node server.js
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询