Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ofxLibwebsocketsからHerokuに接続できない http://blog.hoppenichu.com/javascript/146
WebSocketServer = require("websocket").server
WebSocketClient = require("websocket").client
Http = require("http")
class ServerManager
constructor: ->
@server = {}
@webSocketServer = {}
startServer: ->
@server = Http.createServer((req, res) ->
console.log req.url
res.writeHead(404)
res.end()
)
port = 8080
if 2 < process.argv.length
port = process.argv[2]
@server.listen(port, ->
console.log "Server Listening on Port #{port}"
)
@webSocketServer = new WebSocketServer({
httpServer: @server
})
@webSocketServer.on("request", (req) ->
connection = req.accept()
console.log "Connection #{connection.remoteAddress} Accepted"
connection.on("close", (code, desc) ->
console.log "Connection #{connection.remoteAddress} Closed"
)
)
broadCast: (message) ->
@webSocketServer.connections.forEach((connection, index) ->
console.log "Send Message to #{connection.remoteAddress}"
connection.sendUTF(message.utf8Data)
)
class App
URL = "ws://{Herokuのアドレス}/"
constructor: ->
@serverManager = new ServerManager()
@setup()
setup: ->
@serverManager.startServer()
client = new WebSocketClient()
_self = @
client.on("connect", (connection) ->
console.log "WebSocket Client Connected"
connection.on("close", ->
console.log "WebSocket Client Closed"
client.connect(URL)
)
connection.on("message", (message) ->
console.log "WebSocket Client Message"
_self.serverManager.broadCast(message)
)
)
client.connect(URL)
do ->
new App()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment