Git Product home page Git Product logo

chrome-sockets-extended's Introduction

chrome-sockets-extended

Wrapping the chrome.sockets API with some objects and events and things!

As you may know, working with chrome.sockets is a little bit of a pain if you've got more than one socket going on. I've thrown together this library to extend some of the sockets functionality to give it more of an object-oriented feel.

Let's get started, shall we?

##Usage ###Creating a socket The first step to using any of the chrome-sockets-extended functionality is to first open a socket:

chrome.socketsExtended.tcp.open({}, function(tcpSocket) {
	// The tcpSocket parameter is an object representing a TCP socket that you can do things with!
});

Now you have a TCP socket - pretty cool, huh? What about udp?

chrome.socketsExtended.udp.open({}, function(udpSocket) {
	// This socket is UDP!
});

Wow... that's slick - what if I want a tcpServer socket?

chrome.socketsExtended.tcpServer.open({}, function(tcpServerSocket) {
	// Seeing a pattern?
});

###Doing things with these sockets Once you have a socket opened, you can access the appropriate chrome.sockets APIs against these objects.

tcpSocket.connect('127.0.0.1', 1234, function(result) {
	if (result < 0) {
		// ERROR
	} else {
		// SUCCESS
	}
});
tcpServerSocket.listen('127.0.0.1', 1234, function(result) {
	if (result < 0) {
		// ERROR LISTENING
	} else {
		// SUCCESS
	}
});

###Events ####receive TCP and UDP sockets will emit a 'receive' event when data is received.

socket.on('receive', function(receiveData) {
	var stringData = String.fromCharCode.apply(null, new Uint8Array(receiveData.data));
	console.log('The socket just received this data!', receiveData);
});

####receiveError

####accept TCP server sockets will emit an 'accept' event when a new client connects, this event contains a new tcp socket for that client

// Bind to the accept event!
serverSocket.on('accept', function(clientSocket) {

	// Unpause the socket (because the chrome.sockets API starts these off as paused...)
	clientSocket.setPaused(false, function() {

		// Message to send to client in a string
		var message = 'The price is wrong, Bob!';

		// Convert that to an ArrayBuffer
		var buffer = new ArrayBuffer(message.length);
		var view = new Uint8Array(buffer);
		for(var i = 0; i < message.length; i++) {
			view[i] = message.charCodeAt(i);
		}

		// Send it!
		clientSocket.send(buffer, function() {

			// Close it!
			clientSocket.close();
		});
	});
});

####acceptError

###More Examples ####TCP echo example

// Create server socket
chrome.socketsExtended.tcpServer.open({}, function(serverSocket) {
	
	// Listen on port 4321 on localhost
	serverSocket.listen('127.0.0.1', 4321, function(result) {

		// Setup the accept
		serverSocket.on('accept', function(clientSocket) {

			// Bind the receive event
			clientSocket.on('receive', function(receiveData) {

				// This is the stuff we're going to send back to them!
				var arrayBuffer = receiveData.data;

				clientSocket.send(arrayBuffer, function() {
					clientSocket.close();
				});

			});

			// Unpause the socket
			clientSocket.setPaused(false);
		});

	});

});

chrome-sockets-extended's People

Contributors

grahamkennery avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.