node net socket send 和receive
Q1: 我之前是在C#的client端对server端进行socket发送请求和接受数据,语句如下
socket.Send(frameBuffer, bufferLength, ScoketFlasg.None);
socket.Receive(framBuffer, numBytesRead, HEADER_length, SocketFlags.None);
那么现在在node下面怎么实现同样的效果呢?
Q2: JavaScript怎么将object转化成array? 比如:我有下面的一个class以及它的实例,该怎么表达
public abstract class Command
{
//
// Data members
//
private DeviceBase m_device;
private readonly byte m_code;
private byte m_error;
private Exception m_transporException;
private ManualResetEvent m_completeEvent;
//
// Constructor
//
public Command(DeviceBase device, byte code, ManualResetEvent completeEvent)
{
m_device = device;
m_code = code;
m_completeEvent = completeEvent;
if (m_completeEvent != null)
{
m_completeEvent.Reset();
}
m_error = 0;
m_transporException = null;
}
//
// Properties
//
public DeviceBase HostDevice
{
get
{
return m_device;
}
}
public byte CommandCode
{
get
{
return m_code;
}
}
public byte ErrorCode
{
get
{
return m_error;
}
protected set
{
m_error = value;
}
}
public Exception TransportException
{
get
{
return m_transporException;
}
internal set
{
m_transporException = value;
}
}
public EventWaitHandle CompleteEvent
{
get
{
return m_completeEvent;
}
}
}
3 回复
Q1: 用socket.io Q2:Google搜对象系列化
@godghdai 谢谢你的答复 Q1: nodejs自带的net.socket支持buffer吗? Q2: serialization? 我是需要将一些值转化成bytes然后放到buffer里面,C#里面的实现是这么写的,在nodejs里面怎么写呢?请指教,我接触JavaScript和nodejs才2周,所以有很多基础的地方不是很清楚
public static byte[] UShortToNetworkOrderBytes(ushort value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes);
}
return bytes;
}
这个你可以参考nodejs的stream,比如对写文件,数据格式就是nodejs的Buffer
来自酷炫的 CNodeMD