golang websocket android連線的問題

lywsbcn發表於2017-09-25

import ( “log” “net/http” “golang.org/x/net/websocket” )

func main() {

http.Handle("/", websocket.Handler(listener)) if err := http.ListenAndServe(":1414", nil); err != nil { log.Println("ok") } }

func listener(ws *websocket.Conn) {

var err error for { var request string if err = websocket.Message.Receive(ws, &request); err != nil { log.Println("連線斷開!!") break } log.Println("客戶端請求 : " + request) } } 上面的 websocket 連線 在 ios 客戶端 和 web 瀏覽器上面 連線都沒有問題

但是到了,Android 上就開啟了連線,但是馬上的斷開了連線

安卓程式碼,連其他的 websocket 是可以連線上去的,貼上程式碼 private WebSocketConnection mConnect = new WebSocketConnection();

public void init() {

String url = "ws://192.168.1.227:1414"; try { mConnect.connect(url, new WebSocketHandler() { @Override public void onOpen() { Log.i(TAG, "onOpen: "); mConnect.sendTextMessage("hha"); } @Override public void onTextMessage(String payload) { Log.i(TAG, "onTextMessage: "+payload); } @Override public void onClose(int code, String reason) { Log.i(TAG, "onClose: " + code + "|" + reason); } }); } catch (WebSocketException e) { e.printStackTrace(); } }

09-24 14:16:00.829 8871-8871/com.jh.websoket D/de.tavendo.autobahn.WebSocketConnection: opening handshake received

09-24 14:16:00.829 8871-8871/com.jh.websoket I/fajflajflajflaj: onOpen:

09-24 14:16:00.829 8871-8871/com.jh.websoket D/de.tavendo.autobahn.WebSocketConnection: fail connection [code = 3, reason = WebSockets connection lost

09-24 14:16:00.829 8871-8871/com.jh.websoket D/de.tavendo.autobahn.WebSocketReader: quit

09-24 14:16:00.830 8871-8942/com.jh.websoket D/de.tavendo.autobahn.WebSocketWriter: ended

09-24 14:16:00.831 8871-8871/com.jh.websoket I/fajflajflajflaj: onClose: 3|WebSockets connection lost 09-24 14:16:00.832 8871-8871/com.jh.websoket D/de.tavendo.autobahn.WebSocketConnection: worker threads stopped

上面是報錯資訊

請問,我哪裡做錯了嗎

更多原創文章乾貨分享,請關注公眾號
  • golang websocket android連線的問題
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章