<|start_filename|>examples/webrtc-simple/static/stylesheets/main.css<|end_filename|>
@CHARSET "UTF-8";

* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}

a{
  text-decoration: none;
  color: #2b7;
  font-weight: bold; 
}

a:hover{
  color: #b15;
  transition: 0.1s all ease-in-out;
}

body{
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  min-height:100%;
  height: 100%;
  line-height: 1.5em;
  padding: 1em;
  color: #ddd;
  background-color: #ffffff;  
}

h1,
h2,
h3,
h4,
h5,
h6 {
  margin: 10px 0;
  font-family: inherit;
  font-weight: bold;
  line-height: 1.5em;
  color: inherit;
  text-rendering: optimizelegibility;
}


html {
  font-size: 100%;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
}

button, 
input{
  font-size: 1em;
  padding: 0.5em 1em;
}



div.message{
  margin: 0.5em 0.5em;
  border-radius: 0.3em;
  padding: 0.5em 1em;
  background-color: #eee;
  box-shadow: 0 0 1em rgba(0,0,0,0.3);
  width: 20em;
  color: #444;
}

div.message.from{
  margin: 0.3em 2em;
}

body{
  background-color: #455;
}
.main{
  display: inline-block;
  padding: 2em;
  margin-bottom: 5em;
}

#wslog{
  box-shadow: inset 0 1em 2em -1em rgba(30,50,40,0.5),
              inset 0 -1em 2em -1em rgba(30,50,40,0.5);
  height: 15em;
  width: 100%;
  line-height: 1em;
  overflow-y: auto;
  word-wrap: break-word;
}

#qrcode {
  width:128px;
  height:128px;
  margin-top:15px;
}

<|start_filename|>examples/webrtc-simple/server.js<|end_filename|>
const fastify = require("fastify")();
const path = require("path");

fastify.register(require("fastify-static"), {
  root: path.join(__dirname, "static")
});

var autobahn = require("autobahn");

const PORT = 9008;
const IP_ADDRESS = '127.0.0.1';

var wsList = [];

var wamp = new autobahn.Connection({
  url: `ws://${IP_ADDRESS}:8090/ws/`,
  realm: "wampire_realm"
});

wamp.onopen = function(session) {
  console.log("ON OPEN");

  session.subscribe("room", args => {
    console.log("Event:", args[0]);
  });

  session.publish("room", ["Hello, world!"]);

  // 3) register a procedure for remoting
  function add2(args) {
    console.log("ADD INVOCATION");
    return args[0] + args[1];
  }
  session.register("com.myapp.add2", add2);

  // 4) call a remote procedure
  // session.call("com.myapp.add2", [2, 3]).then(function(res) {
  //   console.log("Result:", res);
  // });
};

wamp.open();

const jsonrpc_schema = {
  body: {
    type: "object",
    properties: {
      jsonrpc: { type: "string" },
      method: { type: "string" },
      params: { type: "object" },
      id: { type: "number" }
    }
  }
};

const opt_schema = {
  body: {
    type: "string"
  }
};

fastify.addHook("onRequest", (req, res, next) => {
  console.log(req.method, req.url, req.headers);
  next();
});

fastify.addHook("preHandler", (req, res, next) => {
  next();
});

fastify.addHook("onSend", (req, res, payload, next) => {
  next();
});

fastify.addHook("onResponse", (res, next) => {
  next();
});

fastify.options("/api", { jsonrpc_schema }, function(request, reply) {
  reply.header("Access-Control-Allow-Origin", "*");
  reply.header("Access-Control-Allow-Methods", "OPTIONS, GET");
  reply.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  reply.send("");
});

fastify.post("/message/:room/:client_id", { opt_schema }, function(
  request,
  reply
) {
  console.log(request.body);
  reply.send({ result: "SUCCESS" });
});

fastify.post(
  "/leave/:room/:client_id",
  function(request, reply) {
    console.log(request.body);
    reply.send({ result: "SUCCESS" });
  }
);

fastify.post(
  "/join/:room",
  function(request, reply) {

    console.log(request.body);
    reply.header("Access-Control-Allow-Origin", "*");
    reply.header("Access-Control-Allow-Methods", "OPTIONS, GET");
    reply.header(
      "Access-Control-Allow-Headers",
      "Origin, X-Requested-With, Content-Type, Accept"
    );

    reply.send({
      params: {
        is_initiator: "true",
        room_link: "https://localhost/r/21204838",
        version_info:
          '{"gitHash": "20cdd7652d58c9cf47ef92ba0190a5505760dc05", "branch": "master", "time": "Fri Mar 9 17:06:42 2018 +0100"}',
        messages: [],
        error_messages: [],
        client_id: "29315775",
        ice_server_transports: "",
        bypass_join_confirmation: "false",
        wss_url: "wss://localhost/ws/",
        media_constraints: '{"audio": true, "video": true}',
        include_loopback_js: "",
        is_loopback: "false",
        offer_options: "{}",
        pc_constraints: '{"optional": []}',
        pc_config:
          '{"rtcpMuxPolicy": "require", "bundlePolicy": "max-bundle", "iceServers": []}',
        wss_post_url: "https://localhost/post",
        // ice_server_url: "https://networktraversal.googleapis.com/v1alpha/iceconfig?key=<KEY>",
        warning_messages: [],
        room_id: "21204838",
        include_rtstats_js:
          '<script src="/js/rtstats.js"></script><script src="/pako/pako.min.js"></script>'
      },
      result: "SUCCESS"
    });
  }
);

console.log(`server listening on ${IP_ADDRESS}:${PORT}`);

const start = async () => {
  try {
    await fastify.listen(PORT, IP_ADDRESS);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();
