Reputation: 1934
I've been tryin to get websockets working in my Android app. I am using OKhttp websockets, an apache proxy and a node server. I finally got a connection, but it connects and then within a few seconds I get the onFailure callback from OKHttp with no cause or message attached to the throwable and the response is null. I can't see anything that would cause the onFailure to run. I don't get a callback to the onClose or onClosing, just the onFailure. My setup is rather simple as this is a test before attempting to implement it into my main app...
SocketConnect.kt
class SocketConnect
private val logger by taggedLogger("Call:SocketConnect")
private val client = OkHttpClient()
private val request = Request.Builder().url("https://myserverhere").build()
private val ws = client.newWebSocket(request, SocketConnectListener())
private inner class SocketConnectListener: WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) {
logger.i { "SocketConnect onOpen" }
super.onOpen(webSocket, response)
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
logger.i { "SocketConnect onClosed, code: $code, reason: $reason" }
}
override fun onMessage(webSocket: webSocket, text: String) {
logger.i { "SocketConnect onMessage $text" }
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response) {
logger.i { "SocketConnect onFailure: cause ${t.cause}, message: ${t.message}" }
logger.i { "SocketConnect onFailure: response $response" }
}
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
logger.i { "SocketConnect onClosing: code: $code, reason: $reason" }
}
}
}
Then my apache config just forwards all upgrade requests to my node server...
<VirtualHost *:443>
//...server name and whatnot
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://127.0.0.1:3005/$1" [P,L]
RewriteCond %{HTTPS:Upgrade} websocket [NC]
RewriteCond %{HTTPS:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://127.0.0.1:3005/$1" [P,L]
//...ssl stuff
</VirtualHost>
And finally my simple node server...
const https = require('https')
const express = require('express')
const bodyParser = require('body-parser')
const webSocket = require('ws')
const port = 3005
const wss = new webSocket.Server({noServer: true})
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true})
app.get('/', (req, res) => {
res.send("Server is up and running")
})
const server = app.listen(port, (err) => {
if(error) return console.log(`Error: ${error}`)
console.log(`Server running on port ${port}`)
})
server.on('upgrade', (request, socket, head) => {
console.log('upgrade request received')
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request)
})
})
wss.on('connection', (ws, req) => {
console.log("connection event happened.")
ws.on('message', (message) => {
console.log("message received")
console.log(message)
})
ws.on('close', () => {
console.log("close event happened")
})
})
Now what I get on my server is
upgrade request received
connection event happened
close event happened
and in android logcat
(OkHttp https://myservernamehere/...:345) SocketConnect onOpen
I Compiler allocated 4666KB to compile void android.view.ViewRootImpl.performTraversals()
E (OkHttp https://myservernamehere/...:345) SocketConnect onFailure: cause null, message: null
(OkHttp https://myservernamehere/...:345) SocketConnect onFailure: response null
So the socket is connecting to the node server, but then almost immediately it calls the onFailure in OkHttp and the connection gets closed, but with no reason, no cause, no message, nothing, just failed. I apologize for the long post, but I wanted to provide as much as possible, just in case I'm making a simple mistake and it could easily be spotted by someone much more familiar with this stuff than I am. Thank you
Upvotes: 0
Views: 13