webrtc via websocket client/server connection issue

Wonder if anyone has any ideas on what I am doing wrong here:

I been following : http://blog.felixhagspiel.de/index.php/posts/create-your-own-videochat-application-with-html-and-javascript

The guide works really well as explained, I have tested it via nodejs and all is working absolutely fine.

I am now trying port the example into groovy. Within a grails plugin and hitting some issues.

So here is where I am : Excuse the state of the actual code its me hitting head against brick walls for a while :)

The owner/server creates room - fine

The client comes along and sends offer (offer received but websocket on client disconnects)

Server/owner receives offer but upon attempting to send back answer - since client disconnected nothing is sent back....

https://github.com/vahidhedayati/playground/blob/master/grails-wschat-plugin/src/groovy/grails/plugin/wschat/WsCamEndpoint.groovy#L72

This is where websocket sends to extended class that parses actions:

https://github.com/vahidhedayati/playground/blob/master/grails-wschat-plugin/src/groovy/grails/plugin/wschat/ChatUtils.groovy#L244

Calls:

https://github.com/vahidhedayati/playground/blob/master/grails-wschat-plugin/src/groovy/grails/plugin/wschat/ChatUtils.groovy#L570

https://github.com/vahidhedayati/playground/blob/master/grails-wschat-plugin/src/groovy/grails/plugin/wschat/ChatUtils.groovy#L598

https://github.com/vahidhedayati/playground/blob/master/grails-wschat-plugin/src/groovy/grails/plugin/wschat/ChatUtils.groovy#L575

private void jsonmessageUser(Session userSession,String msg) {
    userSession.getBasicRemote().sendText(msg as String)
}

private void jsonmessageOther(Session userSession,String msg) {
    Iterator<Session> iterator=camsessions?.iterator()
    if (iterator) {
        while (iterator?.hasNext())  {
            def crec=iterator?.next()
            if (crec.isOpen()) {
                def cuser=crec.getUserProperties().get("camuser").toString()
                def cmuser=crec.getUserProperties().get("camusername").toString()
                println "OM ACTIVE USER : ------- ${cuser}"
                if (!cuser.toString().endsWith(cmuser)) {
                    println "----OTHER ||| ${cmuser} ::  ${msg}"
                    crec.getBasicRemote().sendText(msg as String)
                }
            }
        }
    }
}


private void jsonmessageOwner(Session userSession,String msg) {
    Iterator<Session> iterator=camsessions?.iterator()
    if (iterator) {
        while (iterator?.hasNext())  {
            def crec=iterator?.next()
            if (crec.isOpen()) {
                def cuser=crec.getUserProperties().get("camuser").toString()
                def cmuser=crec.getUserProperties().get("camusername").toString()
                if (cuser.toString().endsWith(cmuser)) {
                    println "----OWNER ||| ${cuser} ::  ${msg}"
                    crec.getBasicRemote().sendText(msg as String)
                }
            }
        }
    }

}

Now this is what happens on the browser:

--- SERVER :

First on server we log into where local.ip and remote.ip is lets say 192.168.1.6 so on both client and server hitting this same ip to ensure its all part of the same connection

http://local.ip.address:8080/testwschat/wsChat/

this gives us a chat user we logged in as ff above

then to access webrtc sender manually

http://local.ip.address:8080/testwschat/wsChat/webrtcsend?user=ff

webkit client.js?compile=false:305
Thu Oct 02 2014 16:48:23 GMT+0100 (BST) Connection successfully established client.js?compile=false:220
offer received, answer will be created client.js?compile=false:255
Object {sdp: "v=0
↵o=- 5155933685262328996 2 IN IP4 127.0.0.1
↵s…5748 label:c32aa4ec-9238-4045-b791-0a94ba741b41
↵", type: "offer"} client.js?compile=false:62
stream added client.js?compile=false:168
2icecandidate send to room ff client.js?compile=false:177
Thu Oct 02 2014 16:48:38 GMT+0100 (BST) Connection was closed client.js?compile=false:231
icecandidate send to room ff client.js?compile=false:177
WebSocket is already in CLOSING or CLOSED state. client.js?compile=false:28
icecandidate send to room ff client.js?compile=false:177
WebSocket is already in CLOSING or CLOSED state. client.js?compile=false:28
icecandidate send to room ff client.js?compile=false:177
WebSocket is already in CLOSING or CLOSED state. 

--- on client side:

First on server we log into http://remote.ip.address:8080/testwschat/wsChat/

this gives us a chat user we logged in as cc above

then to access webrtc receiver manually

http://remote.ip.address:8080/testwschat/wsChat/webrtcrec?user=ff

sending offer to: ff client.js?compile=false:138
Sending 2 client.js?compile=false:38
4icecandidate send to room ff client.js?compile=false:124
Thu Oct 02 2014 16:48:38 GMT+0100 (BST) Connection was closed client.js?compile=false:231
icecandidate send to room ff client.js?compile=false:124
WebSocket is already in CLOSING or CLOSED state. client.js?compile=false:28
icecandidate send to room ff client.js?compile=false:124
WebSocket is already in CLOSING or CLOSED state. client.js?compile=false:28
icecandidate send to room ff client.js?compile=false:124
WebSocket is already in CLOSING or CLOSED state. client.js?compile=false:28
icecandidate send to room ff client.js?compile=false:124
WebSocket is already in CLOSING or CLOSED state. client.js?compile=false:28
icecandidate send to room ff client.js?compile=false:124
WebSocket is already in CLOSING or CLOSED state. client.js?compile=false:28

Actual backend websocket logs as per printlns in above socket code can be seen here:

https://gist.github.com/vahidhedayati/6dbd74a1c4a87d373c05

some more logs this time by logging to console the sendServer values via the webpage, it seems the icecandidate json is not received by websocket. since no logs of this on backend.. maybe its buffer related..

https://gist.github.com/vahidhedayati/918b731788118de348d0 -- server or owner browser logs

https://gist.github.com/vahidhedayati/a51796f3dcbb14088a31 -- client/other user browser logs.

Well guys I think my last investigation has helped me answer my own question it was the buffer size.

So if you are coding in Java/Groovy and want to interact with webrtc. You will probably hit this issue. The fix is to increase your

session.setMaxTextMessageBufferSize(1000000)

When the user opens the websocket

@OnOpen
    public void whenOpening(Session userSession,EndpointConfig c,@PathParam("user") String user,@PathParam("viewer") String viewer) {
        if (loggedIn(user)) {
            userSession.setMaxBinaryMessageBufferSize(1024*512)
            userSession.setMaxTextMessageBufferSize(1000000)
            //userSession.setmaxMessageSize(-1L)
            if (viewer.equals(user)) {
                userSession.getUserProperties().put("camuser", user+":"+user);
            }else{
                userSession.getUserProperties().put("camuser", user+":"+viewer);
            }
            if (!camLoggedIn(user)) {
                userSession.getUserProperties().put("camusername", user);
                camsessions.add(userSession)
            }
        }else{
            log.info "could not find chat user ! ${user}"
        }
    }