import SockJS from 'sockjs'
import http from 'http'
import enableLogging from '../../kite/enableLogging'
import Emitter from '../../kite/emitter'
import Session from './session'

class Server extends Emitter {
  constructor(options) {
    super()

    this.options = options ? options : {}
    this.options.hostname = this.options.hostname || '0.0.0.0'
    if (!this.options.port) throw new Error('port is required!')

    enableLogging(options.name, this, options.logLevel)

    const sockjsOptions = {
      log: (level, message) => {
        this.emit(level, message)
      },
    }

    this.sockjs = SockJS.createServer(sockjsOptions)
    this.server = http.createServer()

    this.sockjs.on('connection', connection => {
      this.emit('debug', 'a new connection', connection)
      this.emit('connection', new Session(connection))
    })

    this.sockjs.installHandlers(this.server, { prefix: options.prefix || '' })

    this.emit('debug', 'starting to listen on server', options)
    this.server.listen(options.port, options.hostname)
  }

  getAddress() {
    return `${this.options.hostname}:${this.options.port}`
  }

  close() {
    if (this.server != null) {
      return this.server.close()
    }
  }
}

Server.scheme = 'http'
Server.secureScheme = 'https'

export default Server

@@ -49,61 +49,8 @@
 tp'%0A
-import enableLogging from '../../kite/enableLogging'%0A
 impo
@@ -123,38 +123,154 @@
 n'%0A%0A
-class Server extends Emitter %7B
+import KiteLogger from '../../KiteLogger'%0A%0Aexport default class Server extends Emitter %7B%0A  static scheme = 'http'%0A  static secureScheme = 'https'%0A
 %0A  c
@@ -287,16 +287,21 @@
 (options
+ = %7B%7D
 ) %7B%0A    
@@ -339,23 +339,8 @@
 ions
- ? options : %7B%7D
 %0A   
@@ -473,41 +473,77 @@
     
-enableLogging(options.name, this,
+this.logger = new KiteLogger(%7B%0A      name: options.name,%0A      level:
  opt
@@ -555,16 +555,23 @@
 logLevel
+,%0A    %7D
 )%0A%0A    c
@@ -638,33 +638,81 @@
     
+if (
 this.
-emit(level, message)
+logger%5Blevel%5D) %7B%0A          this.logger%5Blevel%5D(message)%0A        %7D
 %0A   
@@ -876,30 +876,29 @@
    this.
-emit('
+logger.
 debug
-', 
+(
 'a new c
@@ -1077,22 +1077,21 @@
 his.
-emit('
+logger.
 debug
-', 
+(
 'sta
@@ -1360,81 +1360,4 @@
 %7D%0A%7D%0A
-%0AServer.scheme = 'http'%0AServer.secureScheme = 'https'%0A%0Aexport default Server%0A

use KiteLogger