/**
* Created by 一个老实人 on 2017-09-10.
*/
var ws = require("nodejs-websocket")
var clientCount = 0
var PORT = 3000;
var server = ws.createServer(function (conn) {
console.log("New connection")
clientCount++
conn.nickname = "user"+ clientCount
broadcast (conn.nickname + 'comes in')
conn.on("text", function (str) {
console.log("Received "+str)
broadcast(str)
})
conn.on("close", function (code, reason) {
console.log("Connection closed")
broadcast(conn.nickname + 'left')
})
conn.on("error" , function(err){
console.log("handle err")
console.log(err)
})
}).listen(PORT)
console.log('你已经连上服务器端口为' + PORT)
function broadcast(str){
server.connections.forEach(function(connection) {
connection.sendText(str)
})
}