Skip to content

Instantly share code, notes, and snippets.

@finghine
Created June 12, 2017 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finghine/b914d58681e4062ccc1e30b9dc97e731 to your computer and use it in GitHub Desktop.
Save finghine/b914d58681e4062ccc1e30b9dc97e731 to your computer and use it in GitHub Desktop.
斗鱼弹幕抓取的最小实现
package wit.feng.simplesocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Main {
public static void main(String[] args) throws Exception {
work();
}
public static void work() throws Exception
{
Socket ss = new Socket();
SocketAddress endpoint = new InetSocketAddress("danmu.douyu.com",12604);
ss.connect(endpoint);
String logininfo="type@=loginreq/roomid@=291514/";
OutputStream os = ss.getOutputStream();
os.write(sendMsgWraper(logininfo));
InputStream is = ss.getInputStream();
System.out.println("wait...");
boolean isentergroup = false;
while(!ss.isClosed())
{
readMessage(is);
if(!isentergroup)
{
isentergroup = true;
System.out.println("send joingroup");
String joingrop = "type@=joingroup/rid@=291514/gid@=-9999/";
os.write(sendMsgWraper(joingrop));
}
}
ss.close();
}
public static void readMessage(InputStream is) throws IOException
{
byte [] buf =new byte[4];
int sizereadlen= is.read(buf);
if(sizereadlen !=4)
{
System.out.println("error not read size");
System.exit(0);
}
int size = ntohl(buf);
// System.out.println("size:"+size);
// 不考虑效率
byte [] databuffer = new byte[size];
int alreadyReadLen = 0;
while(alreadyReadLen < size)
{
// System.out.println("start read");
int readlen = is.read(databuffer,alreadyReadLen,size-alreadyReadLen);
// System.out.println("read len:"+readlen);
alreadyReadLen += readlen;
// System.out.println("already total :"+alreadyReadLen);
}
byte [] strbuff = new byte[size-9];
System.arraycopy(databuffer,8,strbuff,0,size-9);
String text = new String(strbuff,"UTF-8");
System.out.println(text);
}
public static byte [] sendMsgWraper(String sendmsg) throws Exception{
byte[] endbys = {0};
byte[] bys = sendmsg.getBytes("UTF-8");
int len = bys.length;
byte [] headersize = htonl(len+9);
byte [] type= htons((short)689);
ByteBuffer byb = ByteBuffer.allocate(len+13);
byb.put(headersize);
byb.put(headersize);
byb.put(type);
byb.putShort((short)0);
byb.put(bys);
byb.put(endbys);
return byb.array();
}
public static int ntohl(byte [] bys)
{
return ByteBuffer.wrap(bys).order(ByteOrder.LITTLE_ENDIAN).getInt();
}
public static byte[] htonl(int i)
{
return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(i).array();
}
public static byte[] htons(short i)
{
return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(i).array();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment