WebSocketを利用したいと思い下記ソースを利用し動作を確認していましたが、
接続先サーバを停止した状態で、メッセージ送信(WebSocket.send)した場合に
例外をハンドルできませんでした。
webSocket.onerrorのalertか
try catchのalertを期待したのですが
どちらのメッセージも表示されません。
どうにかしてメッセージ送信(WebSocket.send)時のエラーを検知する方法が無いでしょうか?
よろしくお願いします。
(※相手先サーバを動かしている状態の基本動作は正常に機能しています。)
■ソース
$(function() {
var endpoint = 'ws://' + location.host + '/endpoint';
var webSocket = null;
$('#connectButton').click(function() {
$("#messageList").empty(); webSocket = new WebSocket(endpoint + '?' + encodeURIComponent($('#roomName').val())); webSocket.onopen = function() { $('#roomName').prop('disabled', true); $('#connectButton').prop('disabled', true); $('#disconnectButton').prop('disabled', false); }; webSocket.onclose = function() { }; webSocket.onmessage = function(message) { $('#messageList').prepend($('<li>').text(message.data)); }; webSocket.onerror = function() { alert('エラーが発生しました。'); };
});
$('#disconnectButton').click(function() {
webSocket.close(); webSocket = null; $('#roomName').prop('disabled', false); $('#connectButton').prop('disabled', false); $('#disconnectButton').prop('disabled', true);
});
$('#sendButton').click(function() {
if (!webSocket) {
alert('未接続です。');
return;
}
try { webSocket.send($('#message').val()); } catch(e) { alert('エラーが発生しました2。'); }
});
});
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。