websocket.js
2.85 KB
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
var stompClient = null;
function setConnected(connected) {
$("#connect").prop("disabled", connected);
$("#disconnect").prop("disabled", !connected);
if (connected) {
$("#conversation").show();
}
else {
$("#conversation").hide();
}
$("#greetings").html("");
}
function connect() {
var socket = new SockJS('/wms/endpoint');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
//console.log('Connected: ' + frame);
stompClient.subscribe('/topic/message', function (greeting) {
// showGreeting(JSON.parse(greeting.body).content);
showGreeting(greeting.body);
});
stompClient.subscribe('/user/topic/message', function (greeting) {
// showGreeting(JSON.parse(greeting.body).content);
showGreeting(greeting.body);
});
});
}
function disconnect() {
if (stompClient !== null) {
stompClient.disconnect();
}
setConnected(false);
//console.log("Disconnected");
}
function sendName() {
// stompClient.send("/app/websocket/message/string", {}, JSON.stringify({'msg': $("#name").val()}));
stompClient.send("/app/websocket/message/string", {}, $("#name").val());
}
function sendJson() {
// stompClient.send("/app/websocket/message/string", {}, JSON.stringify({'msg': $("#name").val()}));
stompClient.send("/app/websocket/message/json", {}, $("#json").val());
}
function sendMsg() {
$.ajax({
url: "/wms/websocket/message/string?msg=" + $("#msg").val(),
// data: formdata,
type: "get",
processData: false,
contentType: false,
success: function(result) {
$.operate.saveSuccess(result);
}
})
}
function sendJson2() {
$.ajax({
url: "/wms/websocket/message/json",
data: $("#json2").val(),
type: "post",
dataType : 'json',
processData: false,
contentType: 'application/json',
success: function(result) {
$.operate.saveSuccess(result);
}
})
}
function showGreeting(message) {
$("#greetings").append("<tr><td>" + message + "</td></tr>");
if(message!=null&&message!=''&&message!=undefined){
let mes=message.split("丨");
if(mes[1]=='TP'){
$("#containerCode").val( mes[0] );
}else if(mes[1]=="NC"){
$("#countryCode").val( mes[0] );
}
}
}
$(function () {
$("form").on('submit', function (e) {
e.preventDefault();
});
$( "#connect" ).click(function() { connect(); });
$( "#disconnect" ).click(function() { disconnect(); });
$( "#send" ).click(function() { sendName(); });
$( "#send2" ).click(function() { sendJson(); });
$( "#send3" ).click(function() { sendMsg(); });
$( "#send4" ).click(function() { sendJson2(); });
});