Commit 15dcf792 authored by Romain Courteaud's avatar Romain Courteaud

Drop the chat application and display connection informations.

Only use this extension as a server.
Applications should be external.
parent 0d2c4410
<a target="_blank" href="https://chrome.google.com/webstore/detail/pkbpddppnkjmlbgliipgmhjeialadokj">![Try it now in CWS](https://raw.github.com/GoogleChrome/chrome-app-samples/master/tryitnowbutton.png "Click here to install this sample from the Chrome Web Store")</a>
# HTTP WebSocket Message BroadCaster
A WebSocket server to broadcast messages to all clients.
# Http WebSocket Server
Can be used for example as a chat application or WebRTC signaling on a LAN.
An example Http and WebSocket server implementation with a sample app which serves itself and allows connected clients to chat with each other. The API is very similar to NodeJS with the intention that this could be an easy drop-in replacement for a NodeJS served application.
Based on Google Chrome [app samples](https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/websocket-server).
## APIs
* [chrome.socket](http://developer.chrome.com/apps/socket.html)
* [Runtime](http://developer.chrome.com/apps/app.runtime.html)
* [Window](http://developer.chrome.com/apps/app.window.html)
## Screenshot
![screenshot](/samples/websocket-server/assets/screenshot_1280_800.png)
......@@ -7,7 +7,11 @@
<script src="index.js"></script>
</head>
<body>
<textarea id="log"></textarea>
<input type="text" id="input" placeholder="Type a message to other connected clients here">
<p>This is a WebSocket server which broadcasts all messages to all connected clients</p>
<h2>Server URLs</h2>
<textarea readonly id="server_url_list"></textarea>
<h2>Peer list</h2>
<p>Connected peers: <span id="client_count">0</span></p>
<textarea readonly id="peer_list"></textarea>
</body>
</html>
/**
* Copyright (c) 2013 The Chromium Authors. All Rights Reserved.
* Copyright (c) 2015 Nexedi SA and Contributors. All Rights Reserved.
* Romain Courteaud <romain@nexedi.com>
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
**/
function $(id) {
return document.getElementById(id);
}
function log(text) {
$('log').value += text + '\n';
function updateStatus(socket_dict) {
var key,
count = 0,
text = "";
for (key in socket_dict) {
if (socket_dict.hasOwnProperty(key)) {
text += socket_dict[key].peer + "\n";
count += 1;
}
}
$('peer_list').value = text;
$('client_count').textContent = count;
}
function showAccessUrl(port) {
chrome.system.network.getNetworkInterfaces(function(result_list) {
var i,
text = "";
if (result_list) {
for (i = 0; i < result_list.length; i++) {
if (result_list[i].prefixLength == 24) {
text += 'ws://' + result_list[i].address + ':' + port + '/';
}
}
document.getElementById('server_url_list').value = text;
}
});
}
var port = 9999;
var isServer = false;
if (http.Server && http.WebSocketServer) {
// Listen for HTTP connections.
var server = new http.Server();
var wsServer = new http.WebSocketServer(server);
server.listen(port);
isServer = true;
showAccessUrl(port);
server.addEventListener('request', function(req) {
var url = req.headers.url;
......@@ -25,60 +60,36 @@ if (http.Server && http.WebSocketServer) {
});
// A list of connected websockets.
var connectedSockets = [];
var socket_dict = {};
wsServer.addEventListener('request', function(req) {
log('Client connected');
var socket = req.accept();
connectedSockets.push(socket);
socket_dict[socket.socketId_] = {
socket: socket
};
chrome.socket.getInfo(socket.socketId_, function (result) {
if ((result !== undefined) && (result.connected)) {
socket_dict[socket.socketId_].peer = result.peerAddress + ':' + result.peerPort;
updateStatus(socket_dict);
}
});
// When a message is received on one socket, rebroadcast it on all
// connected sockets.
socket.addEventListener('message', function(e) {
for (var i = 0; i < connectedSockets.length; i++)
connectedSockets[i].send(e.data);
var key;
for (key in socket_dict) {
if (socket_dict.hasOwnProperty(key)) {
socket_dict[key].socket.send(e.data);
}
}
});
// When a socket is closed, remove it from the list of connected sockets.
socket.addEventListener('close', function() {
log('Client disconnected');
for (var i = 0; i < connectedSockets.length; i++) {
if (connectedSockets[i] == socket) {
connectedSockets.splice(i, 1);
break;
}
}
delete socket_dict[socket.socketId_];
updateStatus(socket_dict);
});
return true;
});
}
document.addEventListener('DOMContentLoaded', function() {
log('This is a test of an HTTP and WebSocket server. This application is ' +
'serving its own source code on port ' + port + '. Each client ' +
'connects to the server on a WebSocket and all messages received on ' +
'one WebSocket are echoed to all connected clients - i.e. a chat ' +
'server. Enjoy!');
// FIXME: Wait for 1s so that HTTP Server socket is listening...
setTimeout(function() {
var address = isServer ? 'ws://localhost:' + port + '/' :
window.location.href.replace('http', 'ws');
var ws = new WebSocket(address);
ws.addEventListener('open', function() {
log('Connected');
});
ws.addEventListener('close', function() {
log('Connection lost');
$('input').disabled = true;
});
ws.addEventListener('message', function(e) {
log(e.data);
});
$('input').addEventListener('keydown', function(e) {
if (ws && ws.readyState == 1 && e.keyCode == 13) {
ws.send(this.value);
this.value = '';
}
});
}, 1e3);
});
{
"name": "WebSocket Server Test",
"description": "Runs an Http and WebSocket server with a quick and dirty test chat application.",
"version": "0.3.1",
"name": "WebSocket Message BroadCaster",
"short_name": "WebSocket-BroadCaster",
"description": "Runs an WebSocket server to broadcast messages to all clients.",
"version": "0.0.2",
"minimum_chrome_version": "44",
"offline_enabled": true,
"manifest_version": 2,
"permissions": [
{"socket": [
"permissions": ["system.network", {
"socket": [
"tcp-connect",
"tcp-listen"]}],
......
{
"sample": "websocket-server",
"files_with_snippets": [ ],
"ios": {"works": true}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment