2015-11-20 179 views
0

我運行我的程序在哪裏使用websocket連接。之前,我的程序運行正常,但今天我得到了錯誤信息websocket連接關閉不清,需要升級426.在客戶端,我使用高速公路絞線版本15.4.0,並且在服務器中,我在節點js中使用了ws websocket。 請給我一些建議。 Thankyou。需要Websocket升級426

這是我的客戶端代碼:

##WebSocket Class 
class MyClientProtocol (WebSocketClientProtocol): 
    def onConnect(self, response): 
     status = "Server connected: {0}".format(response.peer) 
     logData(status) 

    def onOpen(self): 
     status ="WebSocket connection open." 
     logData (status) 

     def SentData(): 
      select() 

      if (numberOfData==0): #jika data kosong koneksi terputus 
       # if there is no data, connection will closed 
       self.sendClose() 

      else: 
       self.sendMessage (dataSent.encode('utf8')) #send data if file not empty 

     SentData() 

    def onMessage(self, payload, isBinary): 
     if isBinary: 
      print("Binary message received: {0} bytes".format(len(payload))) 
     else: 
      # if server sent ack connection will close 
      if (payload.decode('utf8')=="ok"): 
       print("Text message received") 
       status="data sent" 
       logData(status) 
       dataNew[:]=[] 

       update() 
       # get time while sent 
       #p=subprocess.Popen("date", stdout=subprocess.PIPE, shell=True) 
       #output= str(p.communicate()) 
       #logData(output) 

       self.sendClose() 

      else: 
      # if server sent nack, data will resend 
       self.sendMessage(dataSent.encode('utf8')) 

    def onClose(self, wasClean, code, reason): 
     status ="WebSocket connection closed code [{}]: {}".format(code,reason) 
     logData (status) 
     self.factory.reactor.callLater (int(interval),webSocketConnect)#send data every 10 seconds 


#websocket connection function 
def webSocketConnect(): 
    factory = WebSocketClientFactory (u"wss://node-imamabdul-2.c9.io:8080", debug=False) 
    factory.protocol = MyClientProtocol 

    reactor.connectTCP("node-imamabdul-2.c9.io",8080, factory) 

#SIGINT 
def SIGINT_CustomEventHandler(num, frame): 
    k={1:"SIGHUP", 2:"SIGINT"} 
    status="Recieved signal - " + k[num] 
    logData(status) 
    if frame is not None: 
     status="SIGINT at %s:%s"%(frame.f_code.co_name, frame.f_lineno) 
     logData(status) 
    status="In SIGINT Custom Handler Shutting Down ..." 
    logData (status) 
    if num == 2: 
     #status= "shutting down ...." 
     exitFlag=True 
     reactor.stop() 


## main 
if __name__ == '__main__': 

    readID() 

    conn = sqlite3.connect(vcspath+'vcsdb2.db') 
    cur= conn.cursor() 
    create()  
    ## global variables 
    #os.system("hwclock -w -f /dev/rtc1") 
    exitFlag = False 
    Data =[] 
    DataStatus=False 
    #makeDir() 

    webSocketConnect() 
    signal.signal(signal.SIGINT, SIGINT_CustomEventHandler) 
    reactor.run() 

這是我的服務器代碼:

var WebSocketServer = require('ws').Server 
    , wss = new WebSocketServer({ port: process.env.PORT }); 

port = process.env.PORT; 
console.log("PORT :" + port); 
wss.on('connection', function connection(wss) { 
    console.log("connection opened" + wss.listeners()); 

    wss.on('message', function incoming(message) { 
    console.log("connection has message: " + message) 
    var fs = require('fs'); 
    var timestamp = new Date().toString('hex') 
    fs.appendFile('fromclient.csv',"['"+timestamp+"']"+' ' + message+'\n') 

    wss.send(message); 

    }); 
    wss.on('close', function closeSocket() { 
    console.log("connection closed"); 
    }); 
    wss.on('error', function socketError() { 
    console.log("connection has error"); 
    }); 
}); 
+0

嘗試從您的瀏覽器使用javascript連接到您的服務器...可以連接嗎?還是會出現錯誤? (嘗試隔離問題 - 是服務器問題還是客戶端問題)? – Myst

+0

我可以連接到我的服務器。如果我連接atubahn扭曲的V11它也可以連接。 –

+0

對不起,伊瑪目,我不明白你的答案......當你嘗試連接高速公路v11時,你可以連接(工作)?在JavaScript中的原始websockets(無庫),你不能連接(不工作)?當你嘗試使用高速公路v15你不能連接(不工作)? – Myst

回答

0

在你的代碼使用的是加密的WebSockets連接,這裏(Python代碼):

#websocket connection function 
def webSocketConnect(): 
    factory = WebSocketClientFactory (u"wss://node-imamabdul-2.c9.io:8080", debug=False) 
    factory.protocol = MyClientProtocol 

    reactor.connectTCP("node-imamabdul-2.c9.io",8080, factory) 

因爲您使用的高速公路15.4.0與wss,您遇到了the issue (bug) that exists in Autobahn version 15.4.0

該問題在9天前得到修復。

您需要在修復(真正的新版本,如果它被釋放)後將Autobahn更新爲版本,或者在問題(錯誤)引入之前將Autobahn降級爲版本。

另一種選擇是使用ws而不是wss - 這意味着您的連接不會被加密,這不是建議的。

祝你好運!

+0

我想我的問題來了,當我嘗試連接GSM調制解調器。但是,當我連接LAN連接時,我沒有遇到這個問題。 –

+0

謝謝您的建議。如何升級/降級我的高速公路? 但是,當我使用LAN/eth0連接時,我沒有收到該錯誤消息。 –

+0

我認爲你沒有爲你的本地服務器設置SSL ......所以當你連接到本地服務器時,你使用'ws'而不是'wss' ......或者這樣的高速公路錯誤可能是有趣的至於升級,我不知道。你是如何安裝版本15.4.0的?我看到版本0.10.9 ...也許繼續使用舊版本,直到正式版本? – Myst