Insomnia is the subject here, and the poem treats sleep like something cold and out of reach. The opening comparisons are odd and a little puzzling. Sleep is "hard as a beggar's moon," as if it shines bright and distant over someone who has nothing. It is also "hard as joy to a king," which suggests that even a person who seems to have everything might struggle to feel real happiness. In both cases, sleep is something you can see or want but not quite hold.
The last two lines turn to the speaker's night. "All night rising to leave" gives a sense of restless, repeated attempts to get up and get away from the bed. Then comes a playful twist: "counting princesses," a fairy-tale version of counting sheep. The tone is wry rather than desperate. The mood is tired and dreamlike, with a touch of humor, as though the speaker's mind has wandered into a story while the body stays awake.