blob: 57bc8a4f0e54746f08f178f2b450cf2dcd03a567 [file] [log] [blame]
<?xml version = "1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<meta charset="UTF-8">
<head>
<title>NDN Put via WebSocket</title>
<script type="text/javascript" src="../tools/build/ndn-js.js"></script>
<script type="text/javascript">
var ndn = new NDN();
ndn.transport.connectWebSocket(ndn);
var AsyncPutClosure = function AsyncPutClosure() {
// Inherit from Closure.
Closure.call(this);
};
AsyncPutClosure.prototype.upcall = function(kind, upcallInfo) {
if (kind == Closure.UPCALL_FINAL) {
// Do nothing.
} else if (kind == Closure.UPCALL_INTEREST) {
console.log('AsyncPutClosure.upcall() called.');
var content = document.getElementById('content').value;
var interest = upcallInfo.interest;
var nameStr = escape(interest.name.getName());
var si = new SignedInfo();
si.setFields();
var answer = DataUtils.toNumbersFromString(content);
var co = new ContentObject(new Name(nameStr), si, answer, new Signature());
co.sign();
var coBinary = encodeToBinaryContentObject(co);
// If we directly use coBinary.buffer to feed ws.send(), WebSocket
// will end up sending a packet with 10000 bytes of data. That
// is, WebSocket will flush the entire buffer in BinaryXMLEncoder
// regardless of the offset of the Uint8Array. So we have to
// create a new Uint8Array buffer with just the right size and
// copy the content from coBinary to the new buffer.
// ---Wentao
var bytearray = new Uint8Array(coBinary.length);
bytearray.set(coBinary);
upcallInfo.ndn.transport.ws.send(bytearray.buffer);
console.log('ws.send() finised.');
return Closure.RESULT_INTEREST_CONSUMED;
}
return Closure.RESULT_OK;
};
function run() {
var contentName = document.getElementById('contentname').value;
var result = ndn.registerPrefix(new Name(contentName), new AsyncPutClosure());
document.getElementById('result').innerHTML = 'Content name \'' + contentName
+'\' published. Result: ' + result;
}
</script>
</head>
<body >
<form>
<div>
<p>Please Enter a Content Name:</p>
<input id="contentname" type="text" name="CONTENTNAME" value="/wentao.shang/regtest001" />
<p>Please Enter the Content:</p>
<textarea id="content" cols="40" rows="5" name="CONTENT" >This works!</textarea>
<br />
</div>
</form>
<div>
<button onclick="run()">Publish Content</button>
</div>
<p id="result"></p>
</body>
</html>