|
| 1 | +package com.craftsman.websockets; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import com.google.gson.Gson; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import de.tavendo.autobahn.Autobahn; |
| 11 | +import de.tavendo.autobahn.AutobahnConnection; |
| 12 | + |
| 13 | +/** |
| 14 | + * Created by ALI SHADAÏ (Software Craftman) on 15/09/2017. |
| 15 | + */ |
| 16 | + |
| 17 | +public class WsImpl implements Ws { |
| 18 | + |
| 19 | + final String TAG = "Web Socket Impl"; |
| 20 | + |
| 21 | + final AutobahnConnection autobahnConnection = new AutobahnConnection(); |
| 22 | + final List<Payload> subscriptions = new ArrayList<>(); |
| 23 | + String serverUrl; |
| 24 | + |
| 25 | + public WsImpl(String websocketServerUri) { |
| 26 | + serverUrl = websocketServerUri; |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + @Override |
| 31 | + public Ws connect() throws Exception { |
| 32 | + if(serverUrl == null || !serverUrl.startsWith("ws")){ |
| 33 | + throw new Exception("Right server url is not provided"); |
| 34 | + } |
| 35 | + autobahnConnection.connect(serverUrl, new Autobahn.SessionHandler() { |
| 36 | + @Override |
| 37 | + public void onOpen() { |
| 38 | + Log.i(TAG,"Connected"); |
| 39 | + |
| 40 | + for (final Payload payload: subscriptions) { |
| 41 | + //setup listener for all subscribed channel |
| 42 | + if(payload == null || payload.channel == null || payload.channel.isEmpty()) continue; |
| 43 | + else if(payload.channel.startsWith("/")) payload.channel = payload.channel.substring(1); |
| 44 | + autobahnConnection.subscribe(payload.channel, Object.class, new Autobahn.EventHandler() { |
| 45 | + @Override |
| 46 | + public void onEvent(String s, Object o) { |
| 47 | + try { |
| 48 | + payload.listner.onEvent(s, |
| 49 | + (payload.objectType != null) ? |
| 50 | + new Gson().fromJson(o.toString(),payload.objectType) |
| 51 | + : o); |
| 52 | + |
| 53 | + } |
| 54 | + catch (Exception e){ |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onClose(int i, String s) { |
| 64 | + Log.i(TAG,"Disconnected"); |
| 65 | + } |
| 66 | + }); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public <T> Ws on(final String channelPath, final Class<T> exceptedDataType, final WsListner<T> wsListner) { |
| 72 | + |
| 73 | + if(!autobahnConnection.isConnected()){ |
| 74 | + subscriptions.add(new Payload<>(channelPath,exceptedDataType,wsListner)); |
| 75 | + return this; |
| 76 | + } |
| 77 | + else { |
| 78 | + autobahnConnection.subscribe(channelPath, Object.class, new Autobahn.EventHandler() { |
| 79 | + @Override |
| 80 | + public void onEvent(String s, Object o) { |
| 81 | + wsListner.onEvent(s,new Gson().fromJson(o.toString(),exceptedDataType)); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Ws on(String channelPath, final WsListner wsListner) { |
| 91 | + if(!autobahnConnection.isConnected()){ |
| 92 | + subscriptions.add(new Payload<>(channelPath,null,wsListner)); |
| 93 | + return this; |
| 94 | + } |
| 95 | + else autobahnConnection.subscribe(channelPath, Object.class, new Autobahn.EventHandler() { |
| 96 | + @Override |
| 97 | + public void onEvent(String s, Object o) { |
| 98 | + wsListner.onEvent(s, o); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void send(String channelPath, String text) { |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void send(String channelPath, byte[] binary) { |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + final private class Payload<T>{ |
| 116 | + String channel; |
| 117 | + Class<T> objectType; |
| 118 | + WsListner listner; |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + public Payload(String channel, Class<T> objectType, WsListner listner) { |
| 123 | + this.channel = channel; |
| 124 | + this.objectType = objectType; |
| 125 | + this.listner = listner; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments