Blame view

src/main/resources/static/js/websocket.js 2.84 KB
周峰 authored
1
2
3
4
5
6
7
var stompClient = null;

function setConnected(connected) {
    $("#connect").prop("disabled", connected);
    $("#disconnect").prop("disabled", !connected);
    if (connected) {
        $("#conversation").show();
周峰 authored
8
    } else {
周峰 authored
9
10
11
12
13
14
15
16
17
18
19
20
        $("#conversation").hide();
    }
    $("#greetings").html("");
}

function connect() {
    var socket = new SockJS('/wms/endpoint');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        stompClient.subscribe('/topic/message', function (greeting) {
            // showGreeting(JSON.parse(greeting.body).content);
周峰 authored
21
22
23
24
25
            try {
                onGetWebsocket(greeting.body);
            } catch (e) {
                console.log(e);
            }
周峰 authored
26
27
28
29
            showGreeting(greeting.body);
        });
        stompClient.subscribe('/user/topic/message', function (greeting) {
            // showGreeting(JSON.parse(greeting.body).content);
周峰 authored
30
31
32
33
34
            try {
                onGetWebsocket(greeting.body);
            } catch (e) {
                console.log(e);
            }
周峰 authored
35
36
37
38
39
40
41
42
43
44
45
46
47
48
            showGreeting(greeting.body);
        });
    });
}

function disconnect() {
    if (stompClient !== null) {
        stompClient.disconnect();
    }
    setConnected(false);
}

function sendName() {
    // stompClient.send("/app/websocket/message/string", {}, JSON.stringify({'msg': $("#name").val()}));
周峰 authored
49
    stompClient.send("/app/websocket/message/string", {}, $("#name").val());
周峰 authored
50
51
52
53
}

function sendJson() {
    // stompClient.send("/app/websocket/message/string", {}, JSON.stringify({'msg': $("#name").val()}));
周峰 authored
54
    stompClient.send("/app/websocket/message/json", {}, $("#json").val());
周峰 authored
55
56
57
58
59
60
61
62
63
}

function sendMsg() {
    $.ajax({
        url: "/wms/websocket/message/string?msg=" + $("#msg").val(),
        // data: formdata,
        type: "get",
        processData: false,
        contentType: false,
周峰 authored
64
        success: function (result) {
周峰 authored
65
66
67
68
69
70
71
72
73
74
            $.operate.saveSuccess(result);
        }
    })
}

function sendJson2() {
    $.ajax({
        url: "/wms/websocket/message/json",
        data: $("#json2").val(),
        type: "post",
周峰 authored
75
        dataType: 'json',
周峰 authored
76
77
        processData: false,
        contentType: 'application/json',
周峰 authored
78
        success: function (result) {
周峰 authored
79
80
81
82
83
84
85
86
87
88
89
90
91
            $.operate.saveSuccess(result);
        }
    })
}

function showGreeting(message) {
    $("#greetings").append("<tr><td>" + message + "</td></tr>");
}

$(function () {
    $("form").on('submit', function (e) {
        e.preventDefault();
    });
周峰 authored
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    $("#connect").click(function () {
        connect();
    });
    $("#disconnect").click(function () {
        disconnect();
    });
    $("#send").click(function () {
        sendName();
    });
    $("#send2").click(function () {
        sendJson();
    });
    $("#send3").click(function () {
        sendMsg();
    });
    $("#send4").click(function () {
        sendJson2();
    });
周峰 authored
110
111
});