github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/tests/html/lib/worker.js (about)

     1  /* global importScripts, postMessage, onmessage: true, SockJS */
     2  'use strict';
     3  importScripts('sockjs.js');
     4  
     5  var sjs;
     6  
     7  onmessage = function(e) {
     8    var msg = JSON.parse(e.data);
     9  
    10    if (msg.type === 'open') {
    11      sjs = new SockJS(msg.url, null, msg.transports);
    12      sjs.onmessage = function(e) {
    13        postMessage(JSON.stringify({ type: 'message', data: e.data }));
    14      };
    15      sjs.onopen = function() {
    16        postMessage(JSON.stringify({ type: 'open' }));
    17      };
    18      sjs.onclose = function(e) {
    19        postMessage(JSON.stringify({ type: 'close', code: e.code, reason: e.reason }));
    20      };
    21      sjs.onerror = function(e) {
    22        postMessage(JSON.stringify({ type: 'error ', data: e.toString() }));
    23      };
    24      return;
    25    }
    26  
    27    if (msg.type === 'message') {
    28      sjs.send(msg.data);
    29      return;
    30    }
    31  
    32    if (msg.type === 'close') {
    33      sjs.close();
    34      return;
    35    }
    36  };