Last active
August 29, 2015 14:11
-
-
Save sposmen/c53711a2228ffb0c04de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var clients = []; | |
net.createServer(function (socket) { | |
clients.push(socket); | |
socket.on('data', function (data) { | |
broadcast(data, socket); | |
}); | |
socket.on('end', function () { | |
clients.splice(clients.indexOf(socket), 1); | |
}); | |
function broadcast(message, sender) { | |
clients.forEach(function (client) { | |
if (client === sender) return true; | |
client.write(message); | |
}); | |
} | |
}).listen(7000); | |
console.log("Chat server running at port 7000"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.MovieClip; | |
import flash.events.ProgressEvent; | |
import flash.events.IOErrorEvent; | |
import flash.events.SampleDataEvent; | |
import flash.media.Sound; | |
import flash.net.Socket; | |
import flash.utils.ByteArray; | |
public class Reciver extends MovieClip{ | |
private var socket:Socket = new Socket(); | |
private var socketAddress: String = 'localhost'; //TCP Socket address | |
private var socketPort:int = 7000; // TCP Socket port address | |
private var sound:Sound = new Sound(); | |
private var bytes:ByteArray = new ByteArray(); | |
public function Reciver(){ | |
if(socketConnect() == true){ | |
// TODO: more stuff need to be done to the play sound | |
// sound.addEventListener(SampleDataEvent.SAMPLE_DATA, /*??????*/); | |
// sound.play(); | |
} | |
} | |
private function socketConnect():Boolean{ | |
socket.connect(socketAddress, socketPort); | |
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData); | |
socket.addEventListener(IOErrorEvent.IO_ERROR, socketError); | |
return socket.connected; | |
} | |
private function socketData(event:ProgressEvent):void{ | |
if(socket.bytesAvailable >= 8){ | |
socket.readBytes(bytes); | |
bytes.position = 0; | |
// Should be something like this here? | |
// sound.loadCompressedDataFromByteArray(bytes, bytes.bytesAvailable); | |
} | |
} | |
private function socketError(event:IOErrorEvent):void{ | |
trace('socketError - IO Error: ' + event.text); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.MovieClip; | |
import flash.errors.IOError; | |
import flash.events.SampleDataEvent; | |
import flash.events.ProgressEvent; | |
import flash.events.IOErrorEvent; | |
import flash.media.Microphone; | |
import flash.net.Socket; | |
import flash.utils.ByteArray; | |
public class Streamer extends MovieClip{ | |
private var socket:Socket = new Socket(); | |
private var socketAddress:String = 'localhost'; //TCP Socket address | |
private var socketPort:int = 7000; // TCP Socket port address | |
private var microphone:Microphone = new Microphone(); | |
private var microphoneRate:int = 40; | |
private var microphoneEchoSupression:Boolean = true; | |
public function Streamer(){ | |
if(socketConnect() == true){ | |
startMicrophone(); | |
} | |
} | |
private function startMicrophone():void{ | |
microphone.rate = microphoneRate; | |
microphone.setUseEchoSuppression(microphoneEchoSupression); | |
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, socketWrite); | |
} | |
private function socketConnect():Boolean{ | |
socket.connect(socketAddress, socketPort); | |
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData); | |
socket.addEventListener(IOErrorEvent.IO_ERROR, socketError); | |
return socket.connected; | |
} | |
private function socketData(event:ProgressEvent):void{ | |
if(socket.bytesAvailable > 0){ | |
trace('socketData: server sent some data'); | |
} | |
} | |
private function socketError(event:IOErrorEvent):void{ | |
trace('socketError - IO Error: ' + event.text); | |
} | |
private function socketWrite(event:SampleDataEvent):void{ | |
var sample:Number = event.data.readFloat(); | |
var bytes:ByteArray = new ByteArray(); | |
bytes.writeDouble(sample); | |
bytes.position = 0; | |
socket.writeBytes(bytes); //Write the microphone data into the socket | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment