“JS Terhubung ke Websocket” Kode Jawaban

JavaScript Websocket Contoh Kode

var Socket = new WebSocket('ws://' + window.location.hostname + ':81/'); // The '81' here is the Port where the WebSocket server will communicate with
// The instance of the WebSocket() class (i.e. Socket here), must need to be globally defined

Socket.send("pass your data here, and it'll be String"); // This method one can call locally
Pritam Pagla

Javascript Websocket

// npm install --save ws
const WebSocket = require('ws');

const ws = new WebSocket('ws://www.host.com/path');

ws.on('open', function open() {
  ws.send('something');
});

ws.on('message', function incoming(data) {
  console.log(data);
});
feverdreme

JS Terhubung ke Websocket

var exampleSocket = new WebSocket("wss://www.example.com/socketserver", "protocolOne");
Clear Crane

JS Dengarkan Websocket

function listen(fn){
  fn=fn||console.log
  let property=Object.getOwnPropertyDescriptor
  (MessageEvent.prototype,"data")
  const data=property.get
  function lookAtMessage(){ //to replace get function
    let socket=this.currentTarget instanceof WebSocket
    if(!socket){return data.call(this)}
    let msg=data.call(this)
    Object.defineProperty(this,"data",{value:msg}) //anti-loop
    fn({data:msg,socket:this.currentTarget,event:this})
    return msg
  }
  property.get=lookAtMessage
  Object.defineProperty
  (MessageEvent.prototype,"data",property)
}
listen( ({data})=>console.log(data) )
Dead Dog

Jawaban yang mirip dengan “JS Terhubung ke Websocket”

Pertanyaan yang mirip dengan “JS Terhubung ke Websocket”

Lebih banyak jawaban terkait untuk “JS Terhubung ke Websocket” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya