blob: cece056355a32d3fc73c33665b9d5536704b0706 [file] [log] [blame]
Jeff Thompson287a3182012-11-11 18:12:20 -08001<?xml version = "1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3"DTD/xhtml1-strict.dtd">
4<html xmlns = "http://www.w3.org/1999/xhtml">
5<meta charset="UTF-8">
6
7
8<head>
9 <title>NDN Put via WebSocket</title>
10
11 <script type="text/javascript" src="../Helper.js"></script>
12
13 <script type="text/javascript">
14 var ndn = new NDN();
Jeff Thompson5b265a72012-11-12 01:13:08 -080015 ndn.transport.connectWebSocket(ndn);
Jeff Thompson287a3182012-11-11 18:12:20 -080016
17 var AsyncPutClosure = function AsyncPutClosure() {
18 // Inherit from Closure.
19 Closure.call(this);
20 };
21
22 AsyncPutClosure.prototype.upcall = function(kind, upcallInfo) {
23 if (kind == Closure.UPCALL_FINAL) {
24 // Do nothing.
25 } else if (kind == Closure.UPCALL_INTEREST) {
Jeff Thompson287a3182012-11-11 18:12:20 -080026 console.log('AsyncPutClosure.upcall() called.');
27 var content = document.getElementById('content').value;
28 var interest = upcallInfo.interest;
Jeff Thompson5b265a72012-11-12 01:13:08 -080029 var nameStr = escape(interest.name.getName());
Jeff Thompson287a3182012-11-11 18:12:20 -080030
31 var si = new SignedInfo();
32 si.setFields();
33
Jeff Thompson9c9cb642012-11-15 23:07:37 -080034 var answer = DataUtils.toNumbersFromString(content);
Jeff Thompson287a3182012-11-11 18:12:20 -080035
36 var co = new ContentObject(new Name(nameStr), si, answer, new Signature());
37 co.sign();
Wentao Shangc05dc532012-11-19 12:00:33 -080038 var coBinary = encodeToBinaryContentObject(co);
39 // If we directly use coBinary.buffer to feed ws.send(), WebSocket
40 // will end up sending a packet with 10000 bytes of data. That
41 // is, WebSocket will flush the entire buffer in BinaryXMLEncoder
42 // regardless of the offset of the Uint8Array. So we have to
43 // create a new Uint8Array buffer with just the right size and
44 // copy the content from coBinary to the new buffer.
45 // ---Wentao
46 var bytearray = new Uint8Array(coBinary.length);
47 bytearray.set(coBinary);
Jeff Thompson287a3182012-11-11 18:12:20 -080048
Wentao Shangc05dc532012-11-19 12:00:33 -080049 upcallInfo.ndn.transport.ws.send(bytearray.buffer);
50 console.log('ws.send() finised.');
51
52 return Closure.RESULT_INTEREST_CONSUMED;
Jeff Thompson287a3182012-11-11 18:12:20 -080053 }
54 return Closure.RESULT_OK;
55 };
56
57
58 function run() {
59 var contentName = document.getElementById('contentname').value;
60
Jeff Thompson48ba4ff2012-11-12 01:23:13 -080061 var result = ndn.registerPrefix(new Name(contentName), new AsyncPutClosure());
Jeff Thompson287a3182012-11-11 18:12:20 -080062
63 document.getElementById('result').innerHTML = 'Content name \'' + contentName
64 +'\' published. Result: ' + result;
65 }
66
67 </script>
68
69</head>
70<body >
71 <form>
72 <div>
73 <p>Please Enter a Content Name:</p>
74
75 <input id="contentname" type="text" name="CONTENTNAME" value="/wentao.shang/regtest001" />
76
77 <p>Please Enter the Content:</p>
78
79 <textarea id="content" cols="40" rows="5" name="CONTENT" >This works!</textarea>
80
81 <br />
82 </div>
83 </form>
84 <div>
85 <button onclick="run()">Publish Content</button>
86 </div>
87
88 <p id="result"></p>
89
90</body>
91</html>