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