blob: fb3d644807dbc4604ca9682f77f331ed6b575077 [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">
Jeff Burke92dd8e42012-12-08 11:40:31 -08004<!--
5 See COPYING for copyright and distribution information.
6-->
Jeff Thompson287a3182012-11-11 18:12:20 -08007<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 Shang0e291c82012-12-02 23:36:29 -080014 <script type="text/javascript" src="../tools/build/ndn-js.js"></script>
Jeff Thompson287a3182012-11-11 18:12:20 -080015
16 <script type="text/javascript">
17 var ndn = new NDN();
Jeff Thompson5b265a72012-11-12 01:13:08 -080018 ndn.transport.connectWebSocket(ndn);
Jeff Thompson287a3182012-11-11 18:12:20 -080019
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 Thompson287a3182012-11-11 18:12:20 -080029 console.log('AsyncPutClosure.upcall() called.');
30 var content = document.getElementById('content').value;
31 var interest = upcallInfo.interest;
Jeff Thompson5b265a72012-11-12 01:13:08 -080032 var nameStr = escape(interest.name.getName());
Jeff Thompson287a3182012-11-11 18:12:20 -080033
34 var si = new SignedInfo();
35 si.setFields();
36
Jeff Thompson9c9cb642012-11-15 23:07:37 -080037 var answer = DataUtils.toNumbersFromString(content);
Jeff Thompson287a3182012-11-11 18:12:20 -080038
39 var co = new ContentObject(new Name(nameStr), si, answer, new Signature());
40 co.sign();
Wentao Shangc05dc532012-11-19 12:00:33 -080041 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 Thompson287a3182012-11-11 18:12:20 -080051
Wentao Shangc05dc532012-11-19 12:00:33 -080052 upcallInfo.ndn.transport.ws.send(bytearray.buffer);
53 console.log('ws.send() finised.');
54
55 return Closure.RESULT_INTEREST_CONSUMED;
Jeff Thompson287a3182012-11-11 18:12:20 -080056 }
57 return Closure.RESULT_OK;
58 };
59
60
61 function run() {
62 var contentName = document.getElementById('contentname').value;
63
Jeff Thompson48ba4ff2012-11-12 01:23:13 -080064 var result = ndn.registerPrefix(new Name(contentName), new AsyncPutClosure());
Jeff Thompson287a3182012-11-11 18:12:20 -080065
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>