<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://wiki.swoole.com/static/js/jquery.js"></script>
<div>
    我的id:<span id="myId"></span>
</div>
<br>
<div>
    <input type="text" id="Id">
    <input type="text" id="message">
    <button onclick="sendMessage()">发送</button>
</div>
<div>
    
</div>
<script>
var wsServer = 'ws://192.168.120.236:9501';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
    console.log("Connected to WebSocket server.");
};

websocket.onclose = function (evt) {
    console.log("Disconnected");
};

websocket.onmessage = function (evt) {
    console.log('Retrieved data from server:');
    console.log(evt);
    var json = $.parseJSON(evt.data);
    if (json.status) {
        
    } else {
        $("#myId").text(json.socketId);
    }
};

websocket.onerror = function (evt, e) {
    console.log('Error occured: ' + evt.data);
};
function sendMessage(){
    var json = $.parseJSON('{"sendTo":' + $("#Id").val() + ',"message":"' + $("#message").val() + '"}');
    console.log(JSON.stringify(json));
    websocket.send(JSON.stringify(json));
}
</script>