<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="ja">
<head>
<title>WebSocketサンプル</title>
<meta charset="UTF-8" />
<!--[if lt IE 9]>
<script type="text/javascript" src="./js/jquery-1.11.2.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
<script type="text/javascript" src="./js/jquery-2.2.3.js"></script>
<!--<![endif]-->
<script type="text/javascript">
$(function(){
var addRow = function (msg) {
var replace = msg.replace( /\n/g , "<br/>" ) ;
var lastRow = $('#lineTable tbody > tr:last').after(replace);
}
var ws = new WebSocket("ws://arimodoki.dip.jp/j_Labo/wshandle.xhtml");
ws.onopen = function(){
addRow("<tr><td>◆WebSocketが開きました。</td></tr>");
};
ws.onclose = function(){
addRow("<tr><td>◆WebSocketが閉じました。</td></tr>");
};
ws.onmessage = function(message){
addRow(message.data);
};
ws.onerror = function(event){
alert("エラー");
};
$(window).unload(function() {
ws.onclose();
})
$("#send").click(function(){
ws.send($("#message").val());
$("#message").val("");
return false;
});
});
</script>
<style>
<!--
table.bord {
border:solid 1px #cccccc;
padding:5px;
border-collapse:separate;
border-spacing:5px;
width:60%;
height: auto !important;
}
//-->
</style>
</head>
<body>
<table>
<tr>
<td>
<span>WebSocket(ライン風)のサンプルです。</span><br />
<!-- メッセージ表示エリア -->
<table class="bord" id="lineTable">
<tbody>
<tr><td></td></tr>
</tbody>
</table>
<!-- メッセージ表示エリア -->
</td>
</tr>
<tr>
<td>
メッセージ:
<form id="form">
<textarea id="message" cols="40" rows="5"></textarea><br />
<input type="button" id="send" value="送信" />
</form>
</td>
</tr>
</table>
</body>
</html>
|