summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-01 22:27:46 +0000
committersbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-01 22:27:46 +0000
commit8a7916c3d4c009a26dbfcb268b8aad0f9945f5aa (patch)
tree65cafbfc6fe0c29816f323e7d5c7902b7bba9f5a
parent85e61d5ea58f568e31fe763ef4db922d69a20491 (diff)
downloadchromium_src-8a7916c3d4c009a26dbfcb268b8aad0f9945f5aa.zip
chromium_src-8a7916c3d4c009a26dbfcb268b8aad0f9945f5aa.tar.gz
chromium_src-8a7916c3d4c009a26dbfcb268b8aad0f9945f5aa.tar.bz2
[NaCl SDK] Add TCP/UDP socket example.
This example demonstrates how to use the TcpSocket and UdpSocket APIs. It can either talk to an external server, or when run as a packaged app it can create a local TCP echo server. R=noelallen@chromium.org Review URL: https://codereview.chromium.org/19734012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@215132 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--native_client_sdk/src/build_tools/sdk_files.list11
-rwxr-xr-xnative_client_sdk/src/build_tools/test_projects.py3
-rw-r--r--native_client_sdk/src/examples/api/file_io/example.js6
-rw-r--r--native_client_sdk/src/examples/api/socket/example.dsc20
-rw-r--r--native_client_sdk/src/examples/api/socket/example.js170
-rw-r--r--native_client_sdk/src/examples/api/socket/index.html68
-rw-r--r--native_client_sdk/src/examples/api/socket/socket.cc319
-rw-r--r--native_client_sdk/src/examples/api/websocket/example.js2
-rw-r--r--native_client_sdk/src/examples/common.js4
-rw-r--r--native_client_sdk/src/examples/demo/nacl_io/example.js26
-rw-r--r--native_client_sdk/src/examples/tutorial/load_progress/example.js16
11 files changed, 617 insertions, 28 deletions
diff --git a/native_client_sdk/src/build_tools/sdk_files.list b/native_client_sdk/src/build_tools/sdk_files.list
index 955b37c..5f41bb4 100644
--- a/native_client_sdk/src/build_tools/sdk_files.list
+++ b/native_client_sdk/src/build_tools/sdk_files.list
@@ -71,6 +71,15 @@ examples/api/mouse_lock/Makefile
examples/api/mouse_lock/manifest.json
examples/api/mouse_lock/mouse_lock.cc
examples/api/mouse_lock/mouse_lock.h
+examples/api/socket/background.js
+examples/api/socket/common.js
+examples/api/socket/example.js
+examples/api/socket/icon128.png
+examples/api/socket/index.html
+[win]examples/api/socket/make.bat
+examples/api/socket/Makefile
+examples/api/socket/manifest.json
+examples/api/socket/socket.cc
examples/api/url_loader/background.js
examples/api/url_loader/common.js
examples/api/url_loader/example.js
@@ -302,9 +311,9 @@ include/json/reader.h
include/json/value.h
include/json/writer.h
include/KHR/khrplatform.h
+include/nacl_io/error.h
include/nacl_io/event_emitter.h
include/nacl_io/event_listener.h
-include/nacl_io/error.h
include/nacl_io/inode_pool.h
include/nacl_io/ioctl.h
include/nacl_io/kernel_handle.h
diff --git a/native_client_sdk/src/build_tools/test_projects.py b/native_client_sdk/src/build_tools/test_projects.py
index 8007a1f..8aac430 100755
--- a/native_client_sdk/src/build_tools/test_projects.py
+++ b/native_client_sdk/src/build_tools/test_projects.py
@@ -89,6 +89,9 @@ def GetBrowserTesterCommand(desc, toolchain, config):
'--timeout', '30.0', # seconds
# Prevent the infobar that shows up when requesting filesystem quota.
'--browser_flag', '--unlimited-storage',
+ # Some samples need the use the socket API. Enabling this for all
+ # tests should be harmless.
+ '--browser_flag', '--allow-nacl-socket-api=localhost',
]
args.extend(['--serving_dir', GetServingDirForProject(desc)])
diff --git a/native_client_sdk/src/examples/api/file_io/example.js b/native_client_sdk/src/examples/api/file_io/example.js
index 37be1cb..3fdc741 100644
--- a/native_client_sdk/src/examples/api/file_io/example.js
+++ b/native_client_sdk/src/examples/api/file_io/example.js
@@ -113,11 +113,11 @@ function handleMessage(message_event) {
var args = parts.slice(1);
if (command == 'ERR') {
- common.logMessage('Error: ' + args[0] + '\n');
+ common.logMessage('Error: ' + args[0]);
} else if (command == 'STAT') {
- common.logMessage(args[0] + '\n');
+ common.logMessage(args[0]);
} else if (command == 'READY') {
- common.logMessage('Filesystem ready!\n');
+ common.logMessage('Filesystem ready!');
} else if (command == 'DISP') {
// Find the file editor that is currently visible.
var fileEditorEl =
diff --git a/native_client_sdk/src/examples/api/socket/example.dsc b/native_client_sdk/src/examples/api/socket/example.dsc
new file mode 100644
index 0000000..347f0b5
--- /dev/null
+++ b/native_client_sdk/src/examples/api/socket/example.dsc
@@ -0,0 +1,20 @@
+{
+ 'TOOLS': ['newlib', 'glibc', 'pnacl', 'win', 'linux'],
+ 'TARGETS': [
+ {
+ 'NAME' : 'socket',
+ 'TYPE' : 'main',
+ 'SOURCES' : ['socket.cc'],
+ 'LIBS': ['ppapi_cpp', 'ppapi']
+ }
+ ],
+ 'DATA': [
+ 'example.js',
+ ],
+ 'PRE': '''\nCHROME_ARGS = --allow-nacl-socket-api=localhost\n''',
+ 'DEST': 'examples/api',
+ 'NAME': 'socket',
+ 'TITLE': 'socket',
+ 'GROUP': 'API',
+ 'SOCKET_PERMISSIONS': ["tcp-listen:*:*", "tcp-connect", "resolve-host", "udp-bind:*:*", "udp-send-to:*:*"]
+}
diff --git a/native_client_sdk/src/examples/api/socket/example.js b/native_client_sdk/src/examples/api/socket/example.js
new file mode 100644
index 0000000..a7a7535
--- /dev/null
+++ b/native_client_sdk/src/examples/api/socket/example.js
@@ -0,0 +1,170 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Called by the common.js module.
+function attachListeners() {
+ document.getElementById('connectForm').addEventListener('submit', doConnect);
+ document.getElementById('sendForm').addEventListener('submit', doSend);
+ document.getElementById('listenForm').addEventListener('submit', doListen);
+ document.getElementById('closeButton').addEventListener('click', doClose);
+ if (typeof chrome.socket === 'undefined') {
+ document.getElementById('createServer').style.display = 'none';
+ }
+}
+
+// Called by the common.js module.
+function moduleDidLoad() {
+ // The module is not hidden by default so we can easily see if the plugin
+ // failed to load.
+ common.hideModule();
+}
+
+var msgTcpCreate = 't;'
+var msgUdpCreate = 'u;'
+var msgSend = 's;'
+var msgClose = 'c;'
+
+function doConnect(event) {
+ // Send a request message. See also socket.cc for the request format.
+ event.preventDefault();
+ var hostname = document.getElementById('hostname').value;
+ var type = document.getElementById('connect_type').value;
+ common.logMessage(type);
+ if (type == 'tcp')
+
+{
+ common.logMessage(type);
+ common.naclModule.postMessage(msgTcpCreate + hostname);
+}
+ else
+ common.naclModule.postMessage(msgUdpCreate + hostname);
+}
+
+function doSend(event) {
+ // Send a request message. See also socket.cc for the request format.
+ event.preventDefault();
+ var message = document.getElementById('message').value;
+ while (message.indexOf('\\n') > -1)
+ message = message.replace('\\n', '\n');
+ common.naclModule.postMessage(msgSend + message);
+}
+
+var listeningSocket = -1;
+var creatingServer = false;
+
+function runUDPEchoServer(port) {
+ common.logMessage("Listening on UDP port: " + port);
+ chrome.socket.create("udp", {}, function(createInfo) {
+ listeningSocket = createInfo.socketId;
+ creatingServer = false;
+ chrome.socket.bind(listeningSocket,
+ '127.0.0.1',
+ port,
+ function(result) {
+ if (result !== 0) {
+ common.logMessage("Bind failed: " + result);
+ return;
+ }
+
+ var recvCallback = function(recvFromInfo) {
+ if (recvFromInfo.resultCode <= 0) {
+ common.logMessage("recvFrom failed: " + recvFromInfo.resultCode);
+ return;
+ }
+
+ chrome.socket.sendTo(listeningSocket,
+ recvFromInfo.data,
+ recvFromInfo.address,
+ recvFromInfo.port,
+ function(sendToInfo) {
+ if (readInfo.resultCode < 0) {
+ common.logMessage("SentTo failed: " + sendToInfo.bytesWritten);
+ return;
+ }
+ })
+
+ chrome.socket.recvFrom(listeningSocket, recvCallback);
+ }
+
+ chrome.socket.recvFrom(listeningSocket, recvCallback);
+ })
+ })
+}
+
+function runTCPEchoServer(port) {
+ common.logMessage("Listening on TCP port: " + port);
+ chrome.socket.create("tcp", {}, function(createInfo) {
+ listeningSocket = createInfo.socketId;
+ creatingServer = false;
+ chrome.socket.listen(listeningSocket,
+ '0.0.0.0',
+ port,
+ 10,
+ function(result) {
+ if (result !== 0) {
+ common.logMessage("Listen failed: " + result);
+ return;
+ }
+
+ chrome.socket.accept(listeningSocket, function(acceptInfo) {
+ if (result !== 0) {
+ common.logMessage("Accept failed: " + result);
+ return;
+ }
+
+ var newSock = acceptInfo.socketId;
+
+ var readCallback = function(readInfo) {
+ if (readInfo.resultCode < 0) {
+ common.logMessage("Read failed: " + readInfo.resultCode);
+ chrome.socket.destroy(newSock);
+ return;
+ }
+
+ chrome.socket.write(newSock, readInfo.data, function(writeInfo) {})
+ chrome.socket.read(newSock, readCallback);
+ }
+
+ chrome.socket.read(newSock, readCallback);
+ })
+ })
+ })
+}
+
+function doListen(event) {
+ // Listen a the given port.
+ event.preventDefault();
+ if (typeof chrome.socket === 'undefined') {
+ common.logMessage("Local JavaScript echo server is only available in " +
+ "the packaged version of the this example.");
+ return;
+ }
+
+ var port = document.getElementById('port').value;
+ var type = document.getElementById('listen_type').value;
+ port = parseInt(port);
+
+ if (listeningSocket !== -1) {
+ chrome.socket.destroy(listeningSocket);
+ listeningSocket = -1;
+ }
+
+ if (creatingServer)
+ return;
+ creatingServer = true;
+
+ if (type == 'tcp')
+ runTCPEchoServer(port)
+ else
+ runUDPEchoServer(port)
+}
+
+function doClose() {
+ // Send a request message. See also socket.cc for the request format.
+ common.naclModule.postMessage(msgClose);
+}
+
+function handleMessage(message) {
+ common.logMessage(message.data);
+}
diff --git a/native_client_sdk/src/examples/api/socket/index.html b/native_client_sdk/src/examples/api/socket/index.html
new file mode 100644
index 0000000..305e2c6
--- /dev/null
+++ b/native_client_sdk/src/examples/api/socket/index.html
@@ -0,0 +1,68 @@
+<!DOCTYPE html>
+<html>
+<!--
+Copyright (c) 2013 The Chromium Authors. All rights reserved.
+Use of this source code is governed by a BSD-style license that can be
+found in the LICENSE file.
+-->
+<head>
+ <meta http-equiv="Pragma" content="no-cache">
+ <meta http-equiv="Expires" content="-1">
+ <title>{{title}}</title>
+ <script type="text/javascript" src="common.js"></script>
+ <script type="text/javascript" src="example.js"></script>
+</head>
+<body {{attrs}}>
+ <h1>{{title}}</h1>
+ <h2>Status: <code id="statusField">NO-STATUS</code></h2>
+ <p>The socket example demonstrates how to use the TCP and UDP socket API.<br>
+ First set a server address in the form of 'hostname:port', then push the
+ "Connect" button to establish a connection.<br>
+ "Send" button sends the message of the text area to the the remote host.
+ Any data recieved back will be output to the status log.<br>
+ For example, try connecting to a TCP echo server that will reply with
+ whatever bytes you send, or connect to a web server and send a GET
+ request.<br>
+ "Close" button closes the connection.<br>
+ </p>
+
+ <form id="connectForm">
+ <select id="connect_type">
+ <option value="tcp">TCP</option>
+ <option value="udp">UDP</option>
+ </select>
+ <input type="text" id="hostname" size="25"
+ value="google.com:80">
+ <input type="submit" value="Connect">
+ </form>
+
+ <form id="sendForm">
+ <input type="text" id="message" value="GET / HTTP/1.1\n\n" size="50">
+ <input type="submit" value="Send">
+ </form>
+
+ <button id="closeButton">Close</button>
+
+ <div id="createServer">
+ <h2>Create a local server:</h2>
+ <p>The "Listen" button can be used create a local TCP or UDP echo server listenting
+ on the port specified. This is implemented using the 'chrome.socket' javascript
+ API which will only work when this application is run as a packaged app.<br>
+ </p>
+ <p>
+ <form id="listenForm">
+ <select id="listen_type">
+ <option value="tcp">TCP</option>
+ <option value="udp">UDP</option>
+ </select>
+ Port:
+ <input type="text" id="port" value="8080" size="6">
+ <input type="submit" value="Listen">
+ </form>
+ </p>
+ </div>
+
+ <pre id="log" style="font-weight: bold"></pre>
+ <div id="listener"></div>
+</body>
+</html>
diff --git a/native_client_sdk/src/examples/api/socket/socket.cc b/native_client_sdk/src/examples/api/socket/socket.cc
new file mode 100644
index 0000000..7f8b4a5
--- /dev/null
+++ b/native_client_sdk/src/examples/api/socket/socket.cc
@@ -0,0 +1,319 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdio.h>
+#include <string.h>
+#include <sstream>
+
+#include "ppapi/cpp/host_resolver.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/tcp_socket.h"
+#include "ppapi/cpp/udp_socket.h"
+#include "ppapi/cpp/var.h"
+#include "ppapi/utility/completion_callback_factory.h"
+
+#ifdef WIN32
+#undef PostMessage
+// Allow 'this' in initializer list
+#pragma warning(disable : 4355)
+#endif
+
+static const int s_buffer_size = 1024;
+
+class ExampleInstance : public pp::Instance {
+ public:
+ explicit ExampleInstance(PP_Instance instance)
+ : pp::Instance(instance),
+ callback_factory_(this),
+ send_outstanding_(false) {}
+
+ virtual ~ExampleInstance() {}
+ virtual void HandleMessage(const pp::Var& var_message);
+
+ private:
+ bool IsConnected();
+ bool IsUDP();
+
+ void Connect(const std::string& host, bool tcp);
+ void Close();
+ void Send(const std::string& message);
+ void Receive();
+
+ void OnConnectCompletion(int32_t result);
+ void OnResolveCompletion(int32_t result);
+ void OnReceiveCompletion(int32_t result);
+ void OnReceiveFromCompletion(int32_t result, pp::NetAddress source);
+ void OnSendCompletion(int32_t result);
+
+ pp::CompletionCallbackFactory<ExampleInstance> callback_factory_;
+ pp::TCPSocket tcp_socket_;
+ pp::UDPSocket udp_socket_;
+ pp::HostResolver resolver_;
+ pp::NetAddress remote_host_;
+ char receive_buffer_[s_buffer_size];
+ bool send_outstanding_;
+};
+
+#define MSG_CREATE_TCP 't'
+#define MSG_CREATE_UDP 'u'
+#define MSG_SEND 's'
+#define MSG_CLOSE 'c'
+
+void ExampleInstance::HandleMessage(const pp::Var& var_message) {
+ if (!var_message.is_string())
+ return;
+ std::string message = var_message.AsString();
+ // This message must contain a command character followed by ';' and
+ // arguments like "X;arguments".
+ if (message.length() < 2 || message[1] != ';')
+ return;
+ switch (message[0]) {
+ case MSG_CREATE_UDP:
+ // The command 'b' requests to create a UDP connection the
+ // specified HOST.
+ // HOST is passed as an argument like "t;HOST".
+ Connect(message.substr(2), false);
+ break;
+ case MSG_CREATE_TCP:
+ // The command 'o' requests to connect to the specified HOST.
+ // HOST is passed as an argument like "u;HOST".
+ Connect(message.substr(2), true);
+ break;
+ case MSG_CLOSE:
+ // The command 'c' requests to close without any argument like "c;"
+ Close();
+ break;
+ case MSG_SEND:
+ // The command 't' requests to send a message as a text frame. The
+ // message passed as an argument like "t;message".
+ Send(message.substr(2));
+ break;
+ }
+}
+
+bool ExampleInstance::IsConnected() {
+ if (!tcp_socket_.is_null())
+ return true;
+ if (!udp_socket_.is_null())
+ return true;
+
+ return false;
+}
+
+bool ExampleInstance::IsUDP() {
+ return !udp_socket_.is_null();
+}
+
+void ExampleInstance::Connect(const std::string& host, bool tcp) {
+ if (IsConnected()) {
+ PostMessage("Already connected.");
+ return;
+ }
+
+ if (tcp) {
+ if (!pp::TCPSocket::IsAvailable()) {
+ PostMessage("TCPSocket not available");
+ return;
+ }
+
+ tcp_socket_ = pp::TCPSocket(this);
+ if (tcp_socket_.is_null()) {
+ PostMessage("Error creating TCPSocket.");
+ return;
+ }
+ } else {
+ if (!pp::UDPSocket::IsAvailable()) {
+ PostMessage("UDPSocket not available");
+ return;
+ }
+
+ udp_socket_ = pp::UDPSocket(this);
+ if (udp_socket_.is_null()) {
+ PostMessage("Error creating UDPSocket.");
+ return;
+ }
+ }
+
+ if (!pp::HostResolver::IsAvailable()) {
+ PostMessage("HostResolver not available");
+ return;
+ }
+
+ resolver_ = pp::HostResolver(this);
+ if (resolver_.is_null()) {
+ PostMessage("Error creating HostResolver.");
+ return;
+ }
+
+ int port = 80;
+ std::string hostname = host;
+ size_t pos = host.rfind(':');
+ if (pos != std::string::npos) {
+ hostname = host.substr(0, pos);
+ port = atoi(host.substr(pos+1).c_str());
+ }
+
+ pp::CompletionCallback callback = \
+ callback_factory_.NewCallback(&ExampleInstance::OnResolveCompletion);
+ PP_HostResolver_Hint hint = { PP_NETADDRESS_FAMILY_UNSPECIFIED, 0 };
+ resolver_.Resolve(hostname.c_str(), port, hint, callback);
+ PostMessage("Resolving ...");
+}
+
+void ExampleInstance::OnResolveCompletion(int32_t result) {
+ if (result != PP_OK) {
+ PostMessage("Resolve failed.");
+ return;
+ }
+
+ pp::NetAddress addr = resolver_.GetNetAddress(0);
+ PostMessage(std::string("Resolved: ") +
+ addr.DescribeAsString(true).AsString());
+
+ pp::CompletionCallback callback = \
+ callback_factory_.NewCallback(&ExampleInstance::OnConnectCompletion);
+
+ if (IsUDP()) {
+ PostMessage("Binding ...");
+ remote_host_ = addr;
+ PP_NetAddress_IPv4 ipv4_addr = { 0, { 0 } };
+ udp_socket_.Bind(pp::NetAddress(this, ipv4_addr), callback);
+ } else {
+ PostMessage("Connecting ...");
+ tcp_socket_.Connect(addr, callback);
+ }
+}
+
+void ExampleInstance::Close() {
+ if (!IsConnected()) {
+ PostMessage("Not connected.");
+ return;
+ }
+
+ if (tcp_socket_.is_null()) {
+ udp_socket_.Close();
+ udp_socket_ = pp::UDPSocket();
+ } else {
+ tcp_socket_.Close();
+ tcp_socket_ = pp::TCPSocket();
+ }
+
+ PostMessage("Closed connection.");
+}
+
+void ExampleInstance::Send(const std::string& message) {
+ if (!IsConnected()) {
+ PostMessage("Not connected.");
+ return;
+ }
+
+ if (send_outstanding_) {
+ PostMessage("Already sending.");
+ return;
+ }
+
+ uint32_t size = message.size();
+ const char* data = message.c_str();
+ pp::CompletionCallback callback = \
+ callback_factory_.NewCallback(&ExampleInstance::OnSendCompletion);
+ int32_t result;
+ if (IsUDP())
+ result = udp_socket_.SendTo(data, size, remote_host_, callback);
+ else
+ result = tcp_socket_.Write(data, size, callback);
+ std::ostringstream status;
+ if (result < 0) {
+ if (result == PP_OK_COMPLETIONPENDING) {
+ status << "Sending bytes: " << size;
+ PostMessage(status.str());
+ send_outstanding_ = true;
+ } else {
+ status << "Send returned error: " << result;
+ PostMessage(status.str());
+ }
+ } else {
+ status << "Sent bytes synchronously: " << result;
+ PostMessage(status.str());
+ }
+}
+
+void ExampleInstance::Receive() {
+ memset(receive_buffer_, 0, s_buffer_size);
+ if (IsUDP()) {
+ pp::CompletionCallbackWithOutput<pp::NetAddress> callback = \
+ callback_factory_.NewCallbackWithOutput(
+ &ExampleInstance::OnReceiveFromCompletion);
+ udp_socket_.RecvFrom(receive_buffer_, s_buffer_size-1, callback);
+ } else {
+ pp::CompletionCallback callback = \
+ callback_factory_.NewCallback(&ExampleInstance::OnReceiveCompletion);
+ tcp_socket_.Read(receive_buffer_, s_buffer_size-1, callback);
+ }
+}
+
+void ExampleInstance::OnConnectCompletion(int32_t result) {
+ if (result != PP_OK) {
+ std::ostringstream status;
+ status << "Connection failed: " << result;
+ PostMessage(status.str());
+ return;
+ }
+
+ if (IsUDP()) {
+ pp::NetAddress addr = udp_socket_.GetBoundAddress();
+ PostMessage(std::string("Bound to: ") +
+ addr.DescribeAsString(true).AsString());
+ } else {
+ PostMessage("Connected");
+ }
+
+ Receive();
+}
+
+void ExampleInstance::OnReceiveFromCompletion(int32_t result,
+ pp::NetAddress source) {
+ OnReceiveCompletion(result);
+}
+
+void ExampleInstance::OnReceiveCompletion(int32_t result) {
+ if (result < 0) {
+ std::ostringstream status;
+ status << "Receive failed with: " << result;
+ PostMessage(status.str());
+ return;
+ }
+
+ PostMessage(std::string("Received: ") + receive_buffer_);
+ Receive();
+}
+
+void ExampleInstance::OnSendCompletion(int32_t result) {
+ std::ostringstream status;
+ if (result < 0) {
+ status << "Send failed with: " << result;
+ } else {
+ status << "Sent bytes: " << result;
+ }
+ send_outstanding_ = false;
+ PostMessage(status.str());
+}
+
+// The ExampleModule provides an implementation of pp::Module that creates
+// ExampleInstance objects when invoked.
+class ExampleModule : public pp::Module {
+ public:
+ ExampleModule() : pp::Module() {}
+ virtual ~ExampleModule() {}
+
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new ExampleInstance(instance);
+ }
+};
+
+// Implement the required pp::CreateModule function that creates our specific
+// kind of Module.
+namespace pp {
+Module* CreateModule() { return new ExampleModule(); }
+} // namespace pp
diff --git a/native_client_sdk/src/examples/api/websocket/example.js b/native_client_sdk/src/examples/api/websocket/example.js
index ed0b927..7cfa9e3 100644
--- a/native_client_sdk/src/examples/api/websocket/example.js
+++ b/native_client_sdk/src/examples/api/websocket/example.js
@@ -37,5 +37,5 @@ function doClose() {
}
function handleMessage(message) {
- common.logMessage(message.data + '\n');
+ common.logMessage(message.data);
}
diff --git a/native_client_sdk/src/examples/common.js b/native_client_sdk/src/examples/common.js
index 0d355bf..efa140c 100644
--- a/native_client_sdk/src/examples/common.js
+++ b/native_client_sdk/src/examples/common.js
@@ -235,7 +235,7 @@ var common = (function() {
if (logMessageArray.length > kMaxLogMessageLength)
logMessageArray.shift();
- document.getElementById('log').textContent = logMessageArray.join('');
+ document.getElementById('log').textContent = logMessageArray.join('\n');
console.log(message);
}
@@ -273,7 +273,7 @@ var common = (function() {
return;
}
- logMessage('Unhandled message: ' + message_event.data + '\n')
+ logMessage('Unhandled message: ' + message_event.data)
}
/**
diff --git a/native_client_sdk/src/examples/demo/nacl_io/example.js b/native_client_sdk/src/examples/demo/nacl_io/example.js
index 1937548..ef69aa6 100644
--- a/native_client_sdk/src/examples/demo/nacl_io/example.js
+++ b/native_client_sdk/src/examples/demo/nacl_io/example.js
@@ -85,7 +85,7 @@ function fopenResult(filename, filehandle) {
filehandle_map[filehandle] = filename;
addNameToSelectElements('.file-handle', filehandle, filename);
- common.logMessage('File ' + filename + ' opened successfully.\n');
+ common.logMessage('File ' + filename + ' opened successfully.');
}
function fclose(e) {
@@ -96,7 +96,7 @@ function fclose(e) {
function fcloseResult(filehandle) {
var filename = filehandle_map[filehandle];
removeNameFromSelectElements('.file-handle', filehandle, filename);
- common.logMessage('File ' + filename + ' closed successfully.\n');
+ common.logMessage('File ' + filename + ' closed successfully.');
}
function fread(e) {
@@ -107,7 +107,7 @@ function fread(e) {
function freadResult(filehandle, data) {
var filename = filehandle_map[filehandle];
- common.logMessage('Read "' + data + '" from file ' + filename + '.\n');
+ common.logMessage('Read "' + data + '" from file ' + filename + '.');
}
function fwrite(e) {
@@ -119,7 +119,7 @@ function fwrite(e) {
function fwriteResult(filehandle, bytes_written) {
var filename = filehandle_map[filehandle];
common.logMessage('Wrote ' + bytes_written + ' bytes to file ' + filename +
- '.\n');
+ '.');
}
function fseek(e) {
@@ -132,7 +132,7 @@ function fseek(e) {
function fseekResult(filehandle, filepos) {
var filename = filehandle_map[filehandle];
common.logMessage('Seeked to location ' + filepos + ' in file ' + filename +
- '.\n');
+ '.');
}
function stat(e) {
@@ -141,7 +141,7 @@ function stat(e) {
}
function statResult(filename, size) {
- common.logMessage('File ' + filename + ' has size ' + size + '.\n');
+ common.logMessage('File ' + filename + ' has size ' + size + '.');
}
function opendir(e) {
@@ -153,7 +153,7 @@ function opendirResult(dirname, dirhandle) {
dirhandle_map[dirhandle] = dirname;
addNameToSelectElements('.dir-handle', dirhandle, dirname);
- common.logMessage('Directory ' + dirname + ' opened successfully.\n');
+ common.logMessage('Directory ' + dirname + ' opened successfully.');
}
function readdir(e) {
@@ -164,10 +164,10 @@ function readdir(e) {
function readdirResult(dirhandle, ino, name) {
var dirname = dirhandle_map[dirhandle];
if (ino === '') {
- common.logMessage('End of directory.\n');
+ common.logMessage('End of directory.');
} else {
common.logMessage('Read entry ("' + name + '", ino = ' + ino +
- ') from directory ' + dirname + '.\n');
+ ') from directory ' + dirname + '.');
}
}
@@ -181,7 +181,7 @@ function closedirResult(dirhandle) {
delete dirhandle_map[dirhandle];
removeNameFromSelectElements('.dir-handle', dirhandle, dirname);
- common.logMessage('Directory ' + dirname + ' closed successfully.\n');
+ common.logMessage('Directory ' + dirname + ' closed successfully.');
}
function mkdir(e) {
@@ -191,7 +191,7 @@ function mkdir(e) {
}
function mkdirResult(dirname) {
- common.logMessage('Directory ' + dirname + ' created successfully.\n');
+ common.logMessage('Directory ' + dirname + ' created successfully.');
}
/**
@@ -220,7 +220,7 @@ function makeCall(func) {
function handleMessage(message_event) {
var msg = message_event.data;
if (startsWith(msg, 'Error:')) {
- common.logMessage(msg + '\n');
+ common.logMessage(msg);
} else {
// Result from a function call.
var params = msg.split('\1');
@@ -230,7 +230,7 @@ function handleMessage(message_event) {
if (!resultFunc) {
common.logMessage('Error: Bad message ' + funcName +
- ' received from NaCl module.\n');
+ ' received from NaCl module.');
return;
}
diff --git a/native_client_sdk/src/examples/tutorial/load_progress/example.js b/native_client_sdk/src/examples/tutorial/load_progress/example.js
index 06e452d..faceef5 100644
--- a/native_client_sdk/src/examples/tutorial/load_progress/example.js
+++ b/native_client_sdk/src/examples/tutorial/load_progress/example.js
@@ -20,7 +20,7 @@ function domContentLoaded(name, tc, config, width, height) {
// event is always triggered when an <EMBED> tag has a MIME type of
// application/x-nacl.
function moduleDidStartLoad() {
- common.logMessage('loadstart\n');
+ common.logMessage('loadstart');
}
// Progress event handler. |event| contains a couple of interesting
@@ -45,7 +45,7 @@ function moduleLoadProgress(event) {
loadPercentString = 'Computing...';
}
common.logMessage('progress: ' + loadPercentString +
- ' (' + event.loaded + ' of ' + event.total + ' bytes)\n');
+ ' (' + event.loaded + ' of ' + event.total + ' bytes)');
}
// Handler that gets called if an error occurred while loading the NaCl
@@ -53,17 +53,17 @@ function moduleLoadProgress(event) {
// the error, you have to check lastError on the <EMBED> element to find
// out what happened.
function moduleLoadError() {
- common.logMessage('error: ' + common.naclModule.lastError + '\n');
+ common.logMessage('error: ' + common.naclModule.lastError);
}
// Handler that gets called if the NaCl module load is aborted.
function moduleLoadAbort() {
- common.logMessage('abort\n');
+ common.logMessage('abort');
}
// When the NaCl module has loaded indicate success.
function moduleDidLoad() {
- common.logMessage('load\n');
+ common.logMessage('load');
common.updateStatus('LOADED');
}
@@ -74,15 +74,15 @@ function moduleDidLoad() {
// that if the NaCl module loads successfully, you will get both a 'load'
// event and a 'loadend' event.
function moduleDidEndLoad() {
- common.logMessage('loadend\n');
+ common.logMessage('loadend');
var lastError = event.target.lastError;
if (lastError == undefined || lastError.length == 0) {
lastError = '<none>';
}
- common.logMessage('lastError: ' + lastError + '\n');
+ common.logMessage('lastError: ' + lastError);
}
// Handle a message coming from the NaCl module.
function handleMessage(message_event) {
- common.logMessage('Received PostMessage: ' + message_event.data + '\n');
+ common.logMessage('Received PostMessage: ' + message_event.data);
}