我運行我的程序在哪裏使用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");
});
});
嘗試從您的瀏覽器使用javascript連接到您的服務器...可以連接嗎?還是會出現錯誤? (嘗試隔離問題 - 是服務器問題還是客戶端問題)? – Myst
我可以連接到我的服務器。如果我連接atubahn扭曲的V11它也可以連接。 –
對不起,伊瑪目,我不明白你的答案......當你嘗試連接高速公路v11時,你可以連接(工作)?在JavaScript中的原始websockets(無庫),你不能連接(不工作)?當你嘗試使用高速公路v15你不能連接(不工作)? – Myst