SYNCERのロゴ
アイキャッチ画像

WebSocket.binaryType - 送信されるバイナリデータの種類

binaryTypeは、WebSocketのプロパティです。send()でこれからサーバーに送信する予定の、バイナリデータの種類を返すプロパティです。値を代入することで、次回以降に送信するバイナリデータの種類を指定できます。

概要

名前
binaryType
所属
WebSocket
IDL
attribute BinaryType binaryType;

enum BinaryType { "blob", "arraybuffer" };
仕様書
https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-binarytype

説明

送信予定のバイナリデータの種類を表す文字列を返す。値は"blob"(デフォルト)、または"arraybuffer"の2種類です。

チュートリアル

送信するバイナリデータの種類を取得します。

// 通信開始
webSocketObject = new WebSocket( "wss://socket.syncer.jp/connect" ) ;

// プロパティの取得
var result = webSocketObject.binaryType ;

// コンソールで確認
console.log( result ) ;		// "blob"

デモ

WebSocket.binaryTypeのデモです。サーバーに接続後、「取得」をクリックして下さい。

<!-- このコードは編集できます。 -->

<!DOCTYPE html>
<html>
<head>
<style>
	div#result { white-space: pre-wrap ; }
	input { width: 40% ; font-size: 16px ; }
</style>
</head>
<body>
	<p>
		<button id="hoge">接続</button>
		<button id="fuga">切断</button>
		<button id="piyo">取得</button>
	</p>
	<p>
		<input id="data" value="SYNCER">
		<button id="send">送信</button>
	</p>
<hr>
	<div id="result"></div>
<script>
var ws = null ;
var element1 = document.getElementById( "hoge" ) ;
var element2 = document.getElementById( "fuga" ) ;
var element3 = document.getElementById( "piyo" ) ;
var element4 = document.getElementById( "send" ) ;

// 接続
element1.onclick = function () {
	if ( ws && ws.readyState === 1 ) return false ;

	ws = new WebSocket( "wss://socket.syncer.jp/connect" ) ;

	ws.onopen = function() {
		ws.onmessage = function( message ) {
			logUpdate( message.data ) ;
		}

		ws.onclose = function() {
			logUpdate( "通信を切断しました。" ) ;
		}
	}
}

// 切断
element2.onclick = function () {
	if ( ws && ws.readyState === 1 ) ws.close() ;
}

// 取得
element3.onclick = function () {
	var value = ws.binaryType ;
	logUpdate( value, true ) ;
}

// 送信
element4.onclick = function () {
	ws.send( document.getElementById( "data" ).value ) ;
	logUpdate( ws.binaryType, true ) ;
}

var logUpdate = function ( text, noDate ) {
	var resultElement = document.getElementById( "result" ) ;
	resultElement.textContent = text + ( noDate ? "" : "(" + new Date().toLocaleString() + ")" ) + "\n" + resultElement.textContent ;
}
</script>
</body>
</html>

サポート状況

ChromeFirefoxSafariEdgeIEOperaiOS SafariAndroid
15+ 11+ 6.0+ 10+ 15+ 6.0+ 4.4+
  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2017年10月4日 (水)
コンテンツを公開しました。