3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

websocket 入門

Last updated at Posted at 2020-05-26

基本的な構文

var connection = new WebSocket('ws://localhost:8080');

4つのイベント処理と2つのメソッド

var connection = new WebSocket('ws://localhost:8080');
//通信が接続されたとき
connection.onopen = function(e){};
//エラーが発生したとき
connection.onerror = function(e){};
//メッセージを受け取ったとき
connection.onmessage = function(e){};
//通信が切断されたとき
connection.onclose = function(e){};
//データを送信するメソッド
connection.send();
//通信を切断するメソッド
connection.clse();

ボタンクリックでデータを送信する

btn.addEventListener('click',function(e){
    var text = document.getElementById('text');
    connection.send(text.value);
})

テキストデータを受信する

connection.onmessage = function(e){
    console.log(e.data);
    connection.close();
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?