Blame view

src/main/resources/static/js/websocket.js 2.63 KB
周峰 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
20
        //console.log('Connected: ' + frame);
周峰 authored
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        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);
37
    //console.log("Disconnected");
周峰 authored
38
39
40
41
42
43
44
45
46
}

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()}));
周峰 authored
47
    stompClient.send("/app/websocket/message/json", {},  $("#json").val());
周峰 authored
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
}

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>");
79
    $("#countryCode").val( message );
周峰 authored
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}

$(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(); });
});