0.1.6 • Published 10 years ago

socketbridge v0.1.6

Weekly downloads
1
License
MIT
Repository
-
Last release
10 years ago

SocketBridge

SocketBridge is a library that bridges the socket.io and WebSocket.

Usage example

  • WebBrowser <--(socket.io)--> Node.js + SocketBridge <--(WebSocket)--> WebBrowser
  • WebBrowser <--(socket.io)--> Node.js + SocketBridge <--(WebSocket)--> Other Application

Install

npm install socketbridge

Exapmle

Server

var SocketBridgeServer = require('socketbridge').Server;

var socketBridge = new SocketBridgeServer({socketIO: {server: srv}, webSocket: {port:3000}});
socketBridge.on('connection', function(socket) {
	socket.emit('init', {hello: 'world'});

	socket.on('news', function(data) {
		socketBridge.emit('news', data);
	});

	socket.on('message', function(data) {
		socket.broadcast('message', data);
	});

});

Client (WebBrowser: using socket.io)

var socket = io.connect('http://hogehoge.com');
socket.on('connect', function() {
	console.log('connected');
});

socket.on('init', function(data) {
	alert('init');
});

socket.emit('news', 'fugafuga');
socket.emit('message', 'hogehoge');

Note:
When using a port number other than 80

<script src="http://hogehoge:3000/socket.io/socket.io.js"></script>

Client (WebBrowser: using WebSocket)

var socket = new WebSocket('ws://hogehoge.com:3000');
socket.onopen = function() {
	console.log('connected');
};

socket.onmessage = function(e) {
	var data = JSON.parse(e.data);
	if(data.n == 'news') {
		alert('news: ' + data.v);
	} else if(data.n == 'message') {
		alert('message: ' + data.v);
	}
};

Client (Other Application: using WebSocket-Sharp Library)

WebSocket webSocket = new WebSocket("ws://hogehoge.com:3000");

webSocket.OnOpen += (sender, e) => {
	print("connected");	
};

webSocket.OnMessage += (sender, e) => {
	IDictionary data = new (IDictionary) Json.Deserialize(e.Data);
	if(data["n"] == "news") {
		print("news: " + data);
	} else if(data["n"] == "message") {
		print("message: " + data);
	}
};

Usage

Create SocketBridgeServer

var socketBridge = new SocketBridgeServer({socketIO: {server: srv}, webSocket: {port:3000}});

Option

{
		// socket.io Server option
		socketIO: 
		{
			// Create from HTTP server
			server: srv, 

			// OR

			// Create from new server(use port 80)   
			port: 80 
		},

		// WebSocket Server option
		webSocket: 
		{
			// Create from HTTP server
			server: srv,

			// OR

			// Create from new server(use port 3000)
			port: 3000
		}
}

SocketBridge Server API

// Connected
socketBridge.on('connection', function(socket) {
	console.log('Connected');

	// Recieve event
	socket.on('emit', function(msg) {
		// Emits an event to the socket identified.
		socket.emit('message', msg);
	});

	socket.on('emitAll', function(msg) {
		// Emits an event to all connected clients.
		socketBridge.emit('message', msg);
	});

	socket.on('broadcast', function(msg) {
		// Emits an event to all connected clients except this socket.
		socket.broadcast('message', msg);
	});

	socket.on('emitForSio', function(msg) {
		// Emits an event to all connected clients of socket.io connections.
		socketBridge.emitForSocketIO('message', msg);
	});

	socket.on('emitForWs', function(msg) {
		// Emits an event to all connected clients of WebSocket connections.
		socketBridge.emitForWebSocket('message', msg);
	});

	socket.on('broadcastForSio', function(msg) {
		// Emits an event to all connected clients of socket.io connections except this socket.
		socket.broadcastForSocketIO('message', msg);
	});

	socket.on('broadcastForWs', function(msg) {
		// Emits an event to all connected clients of WebSocket connections except this socket.
		socket.broadcastForWebSocket('message', msg);
	});

	//Event called at the time of disconnect.
	socket.on('disconnect', function() {
		console.log('Disconnected!');
	});
});
0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago