Commit 547a26bb authored by sheteng's avatar sheteng

调完了

parent dae6490e
......@@ -42,15 +42,16 @@ public class FirstFragment extends Fragment {
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String appKey = "c981adfde9c4000";
String userName = "user100";
Long userId = 100L;
String userName = "user101";
Long userId = 101L;
String extData = "extData";
Long ts = System.currentTimeMillis();
String data = appKey + "&" + userId + "&" + userName + "&" + ts +
String data = appKey + "&" + userId + "&" + userName + "&" + extData + "&" + ts +
"&wW7cR31PwJseIoZMOs311nljHpwUmujs";
//sign计算在服务器,本地不要暴露appsecrect
String sign = HmacUtil.stringToSign("wW7cR31PwJseIoZMOs311nljHpwUmujs",data);
CCIMClient.getInstance().initSDK(appKey, userName, 100L, ts, sign, new OnConnectListener() {
CCIMClient.getInstance().initSDK(appKey, userName, extData,101L, ts, sign, new OnConnectListener() {
@Override
public void onSocketConnection() {
refreshLogView("connect success");
......@@ -61,6 +62,7 @@ public class FirstFragment extends Fragment {
refreshLogView("disconnect");
}
});
CCIMClient.getInstance().registerMessageListener(new ResultEventCallback<Chat.ChatResponseMessage>() {
@Override
public void onSuccess(Chat.ChatResponseMessage chatResponseMessage) {
......@@ -130,7 +132,7 @@ public class FirstFragment extends Fragment {
binding.sendTextMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CCIMClient.getInstance().sendTextMessage("你好", Long.parseLong(binding.userId.getText().toString()), new ResultCallback() {
CCIMClient.getInstance().sendTextMessage(binding.msg.getText().toString(), Long.parseLong(binding.userId.getText().toString()), new ResultCallback() {
@Override
public void onSuccess() {
refreshLogView("sendTextMessage success");
......@@ -174,17 +176,17 @@ public class FirstFragment extends Fragment {
}
});
// binding.moveContactGroupRequest.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// CCIMClient.getInstance().moveContactGroup(Long.parseLong(binding.groupId.getText().toString()), Long.parseLong(binding.userId.getText().toString()), new ResultCallback() {
// @Override
// public void onSuccess() {
// refreshLogView("moveContactGroupRequest success " );
// }
// });
// }
// });
binding.moveContactGroupRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CCIMClient.getInstance().moveContactGroup(Long.parseLong(binding.groupId.getText().toString()), Long.parseLong(binding.userId.getText().toString()), new ResultCallback() {
@Override
public void onSuccess() {
refreshLogView("moveContactGroupRequest success " );
}
});
}
});
binding.deleteContact.setOnClickListener(new View.OnClickListener() {
@Override
......@@ -226,7 +228,7 @@ public class FirstFragment extends Fragment {
binding.sendChannelMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CCIMClient.getInstance().sendChannelMessage("搞黄色", Long.parseLong(binding.channelId.getText().toString()), new ResultCallback() {
CCIMClient.getInstance().sendChannelMessage(binding.msg.getText().toString(), Long.parseLong(binding.channelId.getText().toString()), new ResultCallback() {
@Override
public void onSuccess() {
refreshLogView("sendChannelMessage success");
......@@ -238,7 +240,6 @@ public class FirstFragment extends Fragment {
binding.startRecordAudioMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CCIMClient.getInstance().startRecordAudioMessage(getActivity());
}
});
......@@ -256,6 +257,56 @@ public class FirstFragment extends Fragment {
}
});
binding.createChannel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Long> userIds = new ArrayList<>();
userIds.add(Long.parseLong(binding.userId.getText().toString()));
CCIMClient.getInstance().createChannel("大世界", "112", false,userIds, new ResultEventCallback<Long>() {
@Override
public void onSuccess(Long aLong) {
binding.channelId.setText(aLong.toString());
refreshLogView("createChannel " + aLong);
}
});
}
});
binding.joinChannel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CCIMClient.getInstance().joinChannel(Long.parseLong(binding.channelId.getText().toString()), new ResultCallback() {
@Override
public void onSuccess() {
refreshLogView("joinChannel ");
}
});
}
});
binding.removeChannelMember.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CCIMClient.getInstance().removeChannelMember(Long.parseLong(binding.channelId.getText().toString()), Long.parseLong(binding.userId.getText().toString()), new ResultCallback() {
@Override
public void onSuccess() {
refreshLogView("removeChannelMember: ");
}
});
}
});
binding.exitChannel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CCIMClient.getInstance().exitChannel(Long.parseLong(binding.channelId.getText().toString()), new ResultCallback() {
@Override
public void onSuccess() {
refreshLogView("exitChannel: ");
}
});
}
});
}
......
......@@ -28,7 +28,19 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="userId"
android:text="2" />
android:text="103" />
<TextView
android:layout_width="wrap_content"
android:text="groupId"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/groupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="channelId"
android:text="1" />
<TextView
android:layout_width="wrap_content"
......@@ -40,9 +52,16 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="channelId"
android:text="1" />
android:text="6857982583817506816" />
</LinearLayout>
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:hint="msg"
android:text="nihao"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
......@@ -172,6 +191,36 @@
android:text="举报违规用户" />
<Button
android:id="@+id/createChannel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="创建频道" />
<Button
android:id="@+id/joinChannel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="加入频道" />
<Button
android:id="@+id/removeChannelMember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="移除频道成员" />
<Button
android:id="@+id/exitChannel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="退出频道" />
<Button
android:id="@+id/sendChannelMessage"
android:layout_width="wrap_content"
......@@ -186,6 +235,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:visibility="gone"
android:text="开始录音" />
<Button
......@@ -194,6 +244,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:visibility="gone"
android:text="录音上传" />
<Button
......@@ -202,6 +253,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:visibility="gone"
android:text="取消上传" />
......
......@@ -16,6 +16,8 @@ import com.permissionx.guolindev.PermissionX;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import ccim.protocol.Channel;
import ccim.protocol.Chat;
import ccim.protocol.Message;
import ccim.protocol.MessageSpec;
......@@ -44,10 +46,10 @@ public class CCIMClient {
return mInstance;
}
public void initSDK(String appKey, String userName, Long userId, Long ts, String sign, OnConnectListener resultCallback) {
public void initSDK(String appKey, String userName,String extData, Long userId, Long ts, String sign, OnConnectListener resultCallback) {
callBackManager = new CallBackManager<>();
socketManageer = new SocketManageer(callBackManager);
socketManageer.initChat(appKey, userName, userId,ts,sign,resultCallback);
socketManageer.initChat(appKey, userName,extData, userId,ts,sign,resultCallback);
}
......@@ -355,7 +357,6 @@ public class CCIMClient {
@Override
public void onSuccess(Chat.ChatResponseMessage chatResponseMessage) {
if (chatResponseMessage.hasSendMsgRes()) {
logger.info(String.valueOf(chatResponseMessage.getSearchRes()));
callback.onSuccess();
}
}
......@@ -367,8 +368,109 @@ public class CCIMClient {
});
}
/**
* 创建频道
*/
public void createChannel(String channelName,String tag,boolean isOpen,List<Long> userIds, ResultEventCallback<Long> callback) {
Channel.CreateChannelRequest.Builder builder = Channel.CreateChannelRequest.newBuilder()
.setName(channelName)
.setTag(tag)
.setOpen(isOpen);
for (Long id : userIds) {
builder.addUserIds(id);
}
int id = socketManageer.sendRequest(builder.build());
callBackManager.addCallBack(id, new ResultEventCallback<Chat.ChatResponseMessage>() {
@Override
public void onSuccess(Chat.ChatResponseMessage chatResponseMessage) {
if (chatResponseMessage.hasCreateChannelRes()) {
logger.info(String.valueOf(chatResponseMessage));
Channel.CreateChannelResponse createChannelRes = chatResponseMessage.getCreateChannelRes();
callback.onSuccess(createChannelRes.getChannelId());
}
}
@Override
public void onFailed(int errorCode) {
callback.onFailed(errorCode);
}
});
}
/**
* 加入频道
*/
public void joinChannel(Long channelId, ResultCallback callback) {
Channel.JoinChannelRequest.Builder builder = Channel.JoinChannelRequest.newBuilder()
.setChannelId(channelId);
int id = socketManageer.sendRequest(builder.build());
callBackManager.addCallBack(id, new ResultEventCallback<Chat.ChatResponseMessage>() {
@Override
public void onSuccess(Chat.ChatResponseMessage chatResponseMessage) {
if (chatResponseMessage.hasJoinChannelRes()) {
logger.info(String.valueOf(chatResponseMessage));
callback.onSuccess();
}
}
@Override
public void onFailed(int errorCode) {
callback.onFailed(errorCode);
}
});
}
/**
* 移除频道成员
*/
public void removeChannelMember(Long channelId,Long userId, ResultCallback callback) {
Channel.RemoveMemberRequest.Builder builder = Channel.RemoveMemberRequest.newBuilder()
.setChannelId(channelId)
.setUserId(userId);
int id = socketManageer.sendRequest(builder.build());
callBackManager.addCallBack(id, new ResultEventCallback<Chat.ChatResponseMessage>() {
@Override
public void onSuccess(Chat.ChatResponseMessage chatResponseMessage) {
if (chatResponseMessage.hasRemoveMemberRes()) {
logger.info(String.valueOf(chatResponseMessage));
callback.onSuccess();
}
}
@Override
public void onFailed(int errorCode) {
callback.onFailed(errorCode);
}
});
}
/**
* 退出频道
*/
public void exitChannel(Long channelId, ResultCallback callback) {
Channel.ExitChannelRequest.Builder builder = Channel.ExitChannelRequest.newBuilder()
.setChannelId(channelId);
int id = socketManageer.sendRequest(builder.build());
callBackManager.addCallBack(id, new ResultEventCallback<Chat.ChatResponseMessage>() {
@Override
public void onSuccess(Chat.ChatResponseMessage chatResponseMessage) {
if (chatResponseMessage.hasRemoveMemberRes()) {
logger.info(String.valueOf(chatResponseMessage));
callback.onSuccess();
}
}
@Override
public void onFailed(int errorCode) {
callback.onFailed(errorCode);
}
});
}
/**
* 发送群组消息
* 发送频道消息
*/
public void sendChannelMessage(String content, Long channelId, ResultCallback callback) {
Message.SendChannelMessageRequest sendChannelMessageRequest = Message.SendChannelMessageRequest.newBuilder()
......
......@@ -2,5 +2,5 @@ package com.ccwangluo.ccim.listener;
public interface OnConnectListener {
void onSocketConnection();
void onSocketDisconnection();
default void onSocketDisconnection(){};
}
......@@ -15,14 +15,17 @@ import com.xuhao.didi.socket.client.sdk.client.OkSocketOptions;
import com.xuhao.didi.socket.client.sdk.client.action.SocketActionAdapter;
import com.xuhao.didi.socket.client.sdk.client.connection.IConnectionManager;
import java.util.logging.Logger;
import ccim.protocol.Channel;
import ccim.protocol.Chat;
import ccim.protocol.Common;
import ccim.protocol.Message;
import ccim.protocol.Push;
public class SocketManageer {
private final Logger logger = Logger.getLogger(CCIMClient.class.getName());
private static final String SERVER_IP = "117.144.156.2";
// private static final String SERVER_IP = "192.168.31.216";
private static final int SERVER_PORT = 8000;
private static final int version = 1;
......@@ -44,11 +47,12 @@ public class SocketManageer {
}
public void connectChatServer(String appKey, String userName, Long userId,Long ts, String sign) {
public void connectChatServer(String appKey, String userName,String extData, Long userId,Long ts, String sign) {
Chat.ConnectRequest builder = Chat.ConnectRequest.newBuilder()
.setUsername(userName)
.setAppKey(appKey)
.setUserId(userId)
.setExtData(extData)
.setTs(ts)
.setSign(sign)
.build();
......@@ -64,18 +68,25 @@ public class SocketManageer {
});
}
public void initChat(String appKey, String userName, Long userId, Long ts, String sign, OnConnectListener resultCallback) {
public void initChat(String appKey, String userName,String extData, Long userId, Long ts, String sign, OnConnectListener resultCallback) {
onConnectListener = resultCallback;
if (chatInfo != null){
OkSocket.open(chatInfo).disconnect();
}
id = 1;
this.chatInfo = initServer(SERVER_IP, SERVER_PORT, new MessageListener() {
@Override
public void onServerConnect() {
connectChatServer(appKey, userName, userId,ts,sign);
// if(id == 1) {
connectChatServer(appKey, userName,extData, userId, ts, sign);
// }
}
@Override
public void onMessageListener(OriginalData data) {
try {
Chat.ChatResponseMessage chatResponseMessage = Chat.ChatResponseMessage.parseFrom(data.getBodyBytes());
logger.info(chatResponseMessage.toString());
int seq = chatResponseMessage.getHeader().getSeq();
ResultEventCallback<Chat.ChatResponseMessage> callBack = callBackManager.getCallBack(seq);
if (callBack != null) {
......@@ -85,9 +96,7 @@ public class SocketManageer {
} else {
callBack.onFailed(chatResponseMessage.getHeader().getCode().getNumber());
}
if (!chatResponseMessage.hasPullOfflineMsgRes()) {
callBackManager.removeCallBack(seq);
}
callBackManager.removeCallBack(seq);
}
else if (seq == 0) { //push消息
callBack.onSuccess(chatResponseMessage);
......@@ -108,7 +117,6 @@ public class SocketManageer {
//获得当前连接通道的参配对象
OkSocketOptions.Builder okOptionsBuilder = new OkSocketOptions.Builder();
okOptionsBuilder.setReaderProtocol(new MyIReaderProtocol());
// okOptionsBuilder.setConnectionHolden(false);
//将新的修改后的参配设置给连接管理器
manager.option(okOptionsBuilder.build());
//调用通道进行连接
......@@ -173,6 +181,14 @@ public class SocketManageer {
builder.setPushChannelMsgAckReq((Push.PushChannelMessageAckRequest) request);
} else if (request instanceof Message.PullOfflineMessageRequest){
builder.setPullOfflineMsgReq((Message.PullOfflineMessageRequest) request);
} else if (request instanceof Channel.CreateChannelRequest){
builder.setCreateChannelReq((Channel.CreateChannelRequest) request);
} else if (request instanceof Channel.JoinChannelRequest){
builder.setJoinChannelReq((Channel.JoinChannelRequest) request);
} else if (request instanceof Channel.RemoveMemberRequest){
builder.setRemoveMemberReq((Channel.RemoveMemberRequest) request);
} else if (request instanceof Channel.ExitChannelRequest){
builder.setExitChannelReq((Channel.ExitChannelRequest) request);
}
OkSocket.open(getChatInfo())
......
package com.ccwangluo.ccim.util;
import android.util.Base64;
import android.util.Log;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.google.common.base.Strings;
import javax.crypto.KeyGenerator;
import android.util.Log;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
......
......@@ -3,9 +3,10 @@ syntax = "proto3";
package ccim.protocol;
message CreateChannelRequest {
string name = 1;
bool open = 2;
repeated int64 user_ids = 3;
string name = 1;
string tag = 2;
bool open = 3;
repeated int64 user_ids = 4;
}
message CreateChannelResponse {
......
......@@ -68,16 +68,18 @@ message ChatResponseMessage {
};
message ConnectRequest {
string app_key = 1;
int64 user_id = 2;
string username = 3;
int64 ts = 4;
string sign = 5;
// sign使用HMAC加密算法,由接入端的服务端计算完成传给接入端的客户端
// sign = HMAC(app_secret).digestToHex(app_key&user_id&username&ts&app_secret)
// ts=CurrentTimeMillis (毫秒)
string app_key = 1;
int64 user_id = 2;
string username = 3;
string ext_data = 4;
int64 ts = 5;
string sign = 6;
// sign使用HMAC加密算法,由接入端的服务端计算完成传给接入端的客户端
// sign = HMAC_SHA256(app_secret).digestToHex(app_key&user_id&username&ext_data&ts&app_secret)
// ts=CurrentTimeMillis (毫秒)
}
message ConnectResponse {
bool connected = 1; //是否连接成功
}
......@@ -85,49 +87,42 @@ message ConnectResponse {
message PullContactsRequest {}
message PullContactsResponse {
message ContactGroup {
int64 group_id = 1; // 群组id
string name = 2; // 群组名称
}
message Contact {
enum Status {
NORMAL = 0; // 正常
MUTED = 1; // 拒绝接收消息
BLACK_LIST = 2; // 黑名单
}
int64 app_id = 1; // 应用id
int64 user_id = 2;
int64 contact_user_id = 3; // 联系人用户id
string contact_user_name = 4; // 联系人用户名字
int64 contact_group_id = 5; // 联系人群组id
int64 private_id = 6; // 私聊对话id
int64 read_msg_id = 7; // 已读消息id
Status status = 8; // 联系人状态
string ext_data = 9; // 扩展数据
message ContactGroup {
int64 group_id = 1; // 群组id
string name = 2; // 群组名称
}
message Contact {
enum Status {
NORMAL = 0; // 正常
MUTED = 1; // 拒绝接收消息
BLACK_LIST = 2; // 黑名单
}
// message Group {
// int64 group_id = 1;
// int64 app_id = 2;
// int64 owner_user_id = 3;
// int32 limited = 4;
// bool muted = 5;
// string name = 6;
// string ext_data = 7;
// }
message Channel {
int64 channel_id = 1;
int64 app_id = 2;
int64 owner_user_id = 3;
int32 limited = 4;
bool muted = 5;
string name = 6;
string ext_data = 7;
enum NetStatus {
ONLINE = 0;
OFFLINE = 1;
}
repeated ContactGroup contact_groups = 2; // 联系人群组列表
repeated Contact contacts = 3; // 联系人列表
// repeated Group groups = 4; // 群组
repeated Channel channels = 4; // 频道
int64 contact_user_id = 1; // 联系人用户id
string contact_user_name = 2; // 联系人用户名字
int64 contact_group_id = 3; // 联系人群组id
int64 private_id = 4; // 私聊对话id
int64 read_msg_id = 5; // 已读消息id
Status status = 6; // 联系人状态
NetStatus net_status = 7; // 网络状态
string ext_data = 8; // 扩展数据
}
message Channel {
int64 channel_id = 1;
int64 app_id = 2;
int64 owner_user_id = 3;
int32 limited = 4;
bool muted = 5;
string name = 6;
string ext_data = 7;
}
repeated ContactGroup contact_groups = 2; // 联系人群组列表
repeated Contact contacts = 3; // 联系人列表
// repeated Group groups = 4; // 群组
repeated Channel channels = 4; // 频道
}
message SearchRequest {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment